Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 288
P. Duw Avatar asked Jan 01 '26 10:01

P. Duw


1 Answers

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

like image 172
Farhad Maleki Avatar answered Jan 02 '26 23:01

Farhad Maleki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!