2

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.

5
  • You could strip each line, replace multiple spaces with a single space and then use split. Commented Jun 18, 2017 at 20:02
  • Try reading this as a CSV with delimiter space Commented Jun 18, 2017 at 20:03
  • 1
    What python version are you using? In 3.6 input with multiple spaces in between like ' 23 545 345 '.split() returns ['23', '545', '345'] Commented Jun 18, 2017 at 20:06
  • @Szymon I'm using 3.4, but if i just use split() like this: array = np.zeros((len(lines),3)) for i in range (len(lines)): array[i,:]=(lines[i].split()) I just get ValueError: cannot copy sequence with size 0 to array axis with dimension 3 Commented Jun 18, 2017 at 20:22
  • Then you probably have an empty line (or multiple) at the end of the file. Filter those out and it will work. Commented Jun 18, 2017 at 21:00

4 Answers 4

6

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

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

Comments

2

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())

3 Comments

This doesn't work for me. When iterating through the lines and using array[i,:]=(lines[i].split()) I get "ValueError: cannot copy sequence with size 0 to array axis with dimension 3" as an error. lines is genereated through this: lines = text_file.read().split('\n') and array is an array with the domensions len(lines) and 3
check the solution of @lukas herman, its a clean way to do it. You can also check what is an intention list in python to understand it !
I found my error, i iterated 1 step too much so that i ended up assigning an empty object to the last field of the array. Your solution works fine. Thanks
2
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

Comments

1

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']

Comments

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.