1

I'm trying to index each data separated by a new line break.

import cvs
with open('some_file.txt') as f:
    data = f.read()
data = data.splitlines()

And I can read it as

print data[0]
>>> 0.00000e+00        1.39984e+23        8.08209e+22        1.34691e+23        7.94736e+07        3.54090e+21        1.61507e+04        0.00000e+00        4.36307e-01        2.53048e-02        1.17516e-03        5.58890e+03        5.06638e+00        0.00000e+00        4.53490e-02        5.94527e-01        4.49423e-01        5.40076e-02        8.84406e-01        1.44792e-05        2.13497e+04        3.38802e+06        3.38397e-04        3.66874e-01        2.09206e-01        3.59185e-01     45536941
print data[1]
>>> 1.00000e+00        1.46478e+23        8.85202e+22        1.07364e+23        5.65863e+07        3.16193e+21        3.11939e+03        0.00000e+00        9.08775e-01        2.01753e-02        9.82056e-04        7.68423e+03        8.29516e+00        0.00000e+00        1.26423e-01        1.68922e-02        9.82179e-01        4.30002e-02        1.21514e+00        2.93802e-06        2.44811e+06        4.00670e+06        2.71861e-05        3.79373e-01        2.31627e-01        2.82576e-01     48923553
print data[:2]
>>> ['        0.00000e+00        1.39984e+23        8.08209e+22        1.34691e+23        7.94736e+07        3.54090e+21        1.61507e+04        0.00000e+00        4.36307e-01        2.53048e-02        1.17516e-03        5.58890e+03        5.06638e+00        0.00000e+00        4.53490e-02        5.94527e-01        4.49423e-01        5.40076e-02        8.84406e-01        1.44792e-05        2.13497e+04        3.38802e+06        3.38397e-04        3.66874e-01        2.09206e-01        3.59185e-01     45536941', '        1.00000e+00        1.46478e+23        8.85202e+22        1.07364e+23        5.65863e+07        3.16193e+21        3.11939e+03        0.00000e+00        9.08775e-01        2.01753e-02        9.82056e-04        7.68423e+03        8.29516e+00        0.00000e+00        1.26423e-01        1.68922e-02        9.82179e-01        4.30002e-02        1.21514e+00        2.93802e-06        2.44811e+06        4.00670e+06        2.71861e-05        3.79373e-01        2.31627e-01        2.82576e-01     48923553']

Each indexed value has a specific quantity I am wanting to index out. For example, data[0] is the 1st particle along with its values and its index number is indicated by data[0][0], positions x,y,z is indicated with data[0][1], data[0][2], and data[0][3] respectively, data[0][4] for its mass, and so on.

Similarly, you retrieve the same quantities from the 2nd particle data[1], the 3rd data[2], and so on through the entire list data[:].

My problem is that I cannot index into each of the particle values as nicely as

posx = data[:][1]
posy = data[:][2]
posz = data[:][3]
...

or split into each column.

posx = [float(row.split()[1]) for row in data] 
posy = [float(row.split()[1]) for row in data] 
posz = [float(row.split()[1]) for row in data] 

given in the format that it is in.

If you would like to reproduce it, I have provided a dropbox link: https://www.dropbox.com/sh/6f0cy4gk8x1k0rm/AADser16cMI9Xhhw3lyP7vWaa?dl=0

vestland
  • 55,229
  • 37
  • 187
  • 305
iron2man
  • 1,787
  • 5
  • 27
  • 39
  • 1
    Once you have found an answer which works for you, please click the tick next to that answer to accept it. This lets other people with your problem know which solution worked for you. – Adi219 Aug 23 '17 at 18:40
  • Possible duplicate of [How to read csv into record array in numpy?](https://stackoverflow.com/questions/3518778/how-to-read-csv-into-record-array-in-numpy) – Scott Mermelstein Aug 23 '17 at 18:41

1 Answers1

2

Add this to your existing code:

NiceArr = []
for item in data:
    NiceArr.append(item.split())

NiceArr will become an array containing arrays, and each inner array will contain the particle values.

You can retrieve each of the particle values the same way you are trying to use in your post.

Adi219
  • 4,712
  • 2
  • 20
  • 43