6

For some reason, I decided to save my list of data as a string. If I use

f = open('tarfile.txt')
for line in f.readlines():
    print line
f.close()

my output looks like:

[  53.7775   13.4375   26.525    48.63    125.      185.      653.    ]    
[  53.7775    13.33625   26.73375   48.68375  125.       185.       653.     ]    
[  53.7775    13.325     27.11375   48.8875   126.       187.       653.     ]    
[  53.7775    13.43625   27.3175    48.92875  126.       187.       653.     ]    
[  53.7775    14.4825    33.07375   51.7325   141.       202.       595.     ]

I would like to read this data in to 2D array. I have searched and tried various methods such as pickle, eval, json, etc but nothing worked

4 Answers 4

7

If you are using numpy (or don't mind using it), you can do numpy.loadtxt('tarfile.txt', usecols=range(1,8)). It is particularly nice if you are going to want your data in a 2-d numpy array anyway.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. Exactly what I needed. Worked flawlessly.
2

First, you need to translate the [] to something python can handle:

import string
table = string.maketrans('[]','  ')

Now you can iterate through your file, translating, splitting and floating:

for line in f:
    print [float(x) for x in line.translate(table).split()]

If you're guaranteed that [ is the first character and ] is the last character on the line, you can do it with slicing:

print [float(x) for x in line[1:-2].split()]  #-2 accounts for the newline too.

Comments

2

My guess is that your input file contains floats separated by whitespace.

To read such a file, strip the [ and ], split each line, and map each field to a float instance.

records = []
for line in f:
    record = [float(field) for field in line.strip().lstrip('[').rstrip(']').split()]
    records.append(record)

Comments

1

I think regexp is the best way to parse your data:

import re
pattern = r'\d+.\d*'
array = []
for line in open('tarfile.txt'):
    array.append(re.findall(pattern, line))

1 Comment

This was clear and helpful and answered my array question as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.