0

There is a txt file with Wolfram Mathematica style of matrix (it can be 1000+ strings):

{-0.00036, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0.0003, -1.00026, 2, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0.0002, -2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

I need to make the nested list from it to make further calculations in python. The result should be -

s=[[[0.00036], [1], [0], [1], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]],[[0.0003],.. and so on

I am totally new with python, can you help me?

Dmitriy Butenko
  • 1,374
  • 1
  • 8
  • 7
  • Is there always exactly one row of the array on one line of the text file? – bodo Sep 11 '13 at 16:47
  • No, sometimes it can jump, but there are still {} – Dmitriy Butenko Sep 11 '13 at 16:49
  • 2
    Your original data as posted under Ashwinis answer contains arithmetic expressions! You should consider using http://stackoverflow.com/questions/2371436/evaluating-a-mathematical-expression-in-a-string or something like that. – bodo Sep 11 '13 at 17:04
  • Yes sorry, i didn't noticed it, my fault. Can you help me to handle with original data? That file on dropbox have all that i need, for sure. – Dmitriy Butenko Sep 11 '13 at 17:13
  • I hope I have addressed that issue with my edits. Please also look at the documentation I referenced. If you feel your problem is solved, please consider accepting one of the answers by clicking the check mark. – bodo Sep 11 '13 at 17:31
  • 1
    @DmitriyButenko: Consider using NumPy if you want to do further calculations. It will provide both speed and flexibility when working with matrices of numbers. – nneonneo Sep 11 '13 at 17:36

3 Answers3

2

eval is evil, but sometimes it has its place. Because your input data contains arithmetic expressions, eval is the easiest way to convert it.

Naturally, do not run this code on untrusted input because it may compromise your computer. (If it's all stuff you generate out of Mathematica, you should be pretty safe).

Here's the code:

m = eval('[' + text.replace('{','[').replace('}',']') + ']')
m = [[[x] for x in row] for row in m]

Running this on the 111.txt file (using text = open('111.txt').read()) yields the expected result:

[[[-0.00036], [1], [0], [1], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]], [[0.0003], [-1.00026], [2], [0], [1], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]], [[0], [0.0002], [-2], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]], [[1e-05], [0], [0], [-1], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]], [[0], [1e-05], [0], [0], [-1], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]], [[5e-05], [0], [0], [0], [0], [-0.01], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]], [[0], [5e-05], [0], [0], [0], [0], [-0.01], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]], [[0], [0], [0], [0], [0], [0.01], [0], [-0.000344], [1], [0], [1], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]], [[0], [0], [0], [0], [0], [0], [0.01], [0.0003], [-1.000244], [2], [0], [1], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]], [[0], [0], [0], [0], [0], [0], [0], [0], [0.0002], [-2], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]], [[0], [0], [0], [0], [0], [0], [0], [1e-05], [0], [0], [-1], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]], [[0], [0], [0], [0], [0], [0], [0], [0], [1e-05], [0], [0], [-1], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]], [[0], [0], [0], [0], [0], [0], [0], [3.4e-05], [0], [0], [0], [0], [-0.01], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]], [[0], [0], [0], [0], [0], [0], [0], [0], [3.4e-05], [0], [0], [0], [0], [-0.01], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]], [[0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0.01], [0], [-0.000328], [1], [0], [1], [0], [0], [0], [0], [0], [0]], [[0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0.01], [0.0003], [-1.000228], [2], [0], [1], [0], [0], [0], [0], [0]], [[0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0.0002], [-2], [0], [0], [0], [0], [0], [0], [0]], [[0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [1e-05], [0], [0], [-1], [0], [0], [0], [0], [0], [0]], [[0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [1e-05], [0], [0], [-1], [0], [0], [0], [0], [0]], [[0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [1.8e-05], [0], [0], [0], [0], [-0.01], [0], [0], [0], [0]], [[0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [1.8e-05], [0], [0], [0], [0], [-0.01], [0], [0], [0]], [[0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0.01], [0], [-0.0003], [1], [0]], [[0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0.01], [0.0003], [-1.0002], [2]], [[0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0.0002], [-2]]]
nneonneo
  • 171,345
  • 36
  • 312
  • 383
1
from ast import literal_eval
with open('filename') as f:
    lis = [[[abs(literal_eval(item))] for item in 
                     line.rstrip('},\n').lstrip('{').split(', ')] for line in f]
    print lis
...     
[[[0.00036], [1], [0], [1], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]], [[0.0003], [1.00026], [2], [0], [1], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]], [[0], [0.0002], [2], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]]]
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • `Traceback (most recent call last): File "/Users/TwilightWind/Desktop/mas/prog.pyw", line 6, in item in line.rstrip('},\n').lstrip('{').split(', ')] for line in f] File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ast.py", line 49, in literal_eval File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ast.py", line 37, in parse File "", line 1 0}, ^ SyntaxError: invalid syntax [Finished]` – Dmitriy Butenko Sep 11 '13 at 16:56
  • 1
    @DmitriyButenko Please you post the actual content of the file, my answer works fine for the sample data. – Ashwini Chaudhary Sep 11 '13 at 16:58
  • @Ashwini: In the comments the OP says that rows may span multiple lines. That might be the problem. – unutbu Sep 11 '13 at 17:00
  • @unutbu Actual data contains something like `3.40*0.00001`, which is I guess raising the error. – Ashwini Chaudhary Sep 11 '13 at 17:03
  • @AshwiniChaudhary just cleared the matrix from n*m like structures, but that didn't help. Anyways such structures can also have place. – Dmitriy Butenko Sep 11 '13 at 17:08
  • 1
    @Ashwini: The lines end with `\r`, not `\n`. – unutbu Sep 11 '13 at 17:08
  • @DmitriyButenko replace `line.rstrip('},\n')` with `line.rstrip('},\r\n').` – Ashwini Chaudhary Sep 11 '13 at 18:17
1

Here is some code which allows the line breaks to be anywhere you like. But the entries can only be floating point numbers.

f = open("filename", "r")

buff = ""
array = []
for line in f:
    buff += line
    if "}" in line:
        start = buff.index("{")
        end = buff.index("}")
        array.append([float(val.strip()) for val in buff[start+1:end].split(",")])
        buff = buff[end+1:]

f.close()

print(array)

If you need arithmetic expression to the extend of one level of multiplication, you can do:

import operator

def prod(lst):
    return reduce(operator.mul, lst, 1)

f = open("filename", "r")

buff = ""
array = []
for line in f:
    buff += line
    if "}" in line:
        start = buff.index("{")
        end = buff.index("}")
        array.append([prod(float(factor.strip()) for factor in val.split("*")) for val in buff[start+1:end].split(",")])
        buff = buff[end+1:]

f.close()

print(array)

To understand what is going on, look at List Comprehensions and str.split for a start.

bodo
  • 1,005
  • 15
  • 31