I am trying to extract data from a text file and place it into a 2D array where the columns are organized by what value they represent so I can plot the data and analyze it. The data in the text file is formatted like
10.24284 49.89447 462.90 312.4 Wed Dec 7, 2016 6:42:10p EST
I want to be able to separate the values in each column into their own lists or a full 2D array. I have tried looking into open('filename')
as well as readlines
but it just returns a mess of numbers that are not sorted in any way. What is the best solution to the problem?
Using open('filename', 'r')
(the r means read) you can loop over all the lines in the code with a simple for loop. something like this:
with open('filename', 'r') as inputfile:
for line in inputfile:
#do something with the string
You said that the data you had was formatted like this:
10.24284 49.89447 462.90 312.4 Wed Dec 7, 2016 6:42:10p EST
you could take each line and split it on every space like this:
line.split(" ")
you would now have something like:
['10.24284', '49.89447', '462.90', '312.4', 'Wed', 'Dec', '7,', '2016', '6:42:10p', 'EST']
if you wanted to keep the date together in the final array you could limit how many time you split it like this:
line.split(" ", 4)
this would give you:
['10.24284', '49.89447', '462.90', '312.4', 'Wed Dec 7, 2016 6:42:10p EST']
The numbers part is trivial...
result = [map(float, L.split()[:4]) for L in open("datafile.txt")]
result[43][2]
will be the third number on 44-th row (counting starts from 0 in Python)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.