P. Duw
P. Duw

Reputation: 11

How to read formatted file in Python array

I need to read data from a file that's formatted like this:

  0.00000  62.12404   0.00000
  1.95695  62.12288   0.00000
  3.91389  62.11939   0.00000
  5.87084  62.11357   0.00000
  7.82779  62.10543   0.00000
  9.78474  62.09496   0.00000
 11.74168  62.08218   0.00000
 13.69863  62.06707   0.00000

(the script that produces the data specifies the format as "%9.5f").The number of lines isn't fixed and I want to have either an 3xN array or 3 arrays of length N at the end. Normally i'd use lines.split but that doesn't really work if the number of spaces between the numbers isn't fixed.

Upvotes: 1

Views: 11485

Answers (4)

Farhad Maleki
Farhad Maleki

Reputation: 3627

The elegant way:

You can read the file using pandas.read_csv method (link to the documentation page). Using an existing module that has been widely tested, documented, and used should always be the first option to be considered to accomplish any task.

Note: You can handle several consecutive spaces using sep='\s+'

The ugly way (reinventing the wheel):

split method from str class can handle several consecutive spaces.

If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a None separator returns [].

Reference

Upvotes: 6

Lukas Herman
Lukas Herman

Reputation: 170

with open("data.txt", "r") as data:
    [line.split() for line in data]

I tested in python 2.7 and python 3.5. It should work

Upvotes: 1

Alper Fırat Kaya
Alper Fırat Kaya

Reputation: 2359

data.txt contains your data

file_object  = open("data.txt", "r")

mylist = list()

while True:
    a = file_object.readline().split()
    if a is None or len(a) == 0 or a is EOFError:
        break
    else:
       mylist.append(a)

for a in mylist:
    print(a)

this code gives the result below

['0.00000', '62.12404', '0.00000']
['1.95695', '62.12288', '0.00000']
['3.91389', '62.11939', '0.00000']
['5.87084', '62.11357', '0.00000']
['7.82779', '62.10543', '0.00000']
['9.78474', '62.09496', '0.00000']
['11.74168', '62.08218', '0.00000']
['13.69863', '62.06707', '0.00000']

Upvotes: 0

Kayf
Kayf

Reputation: 36

split should be works in python 2 and 3 :

>>> str = '  0.00000  62.12404   0.00000'
>>> print str.split()
['0.00000', '62.12404', '0.00000']

You can also try with regex :

print re.split('\s{1,}', str.strip())

Upvotes: 1

Related Questions