1

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?

2
  • Is your input file well-structured? You could try using a CSV format for the input file to make it easier to extract the columns for each entry line. Commented Dec 12, 2016 at 20:10
  • Split each line, and store those arrays into an array => 2D array? Commented Dec 12, 2016 at 20:11

2 Answers 2

2

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']
Sign up to request clarification or add additional context in comments.

Comments

0

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)

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.