2

I have a FILE.txt with several columns:

A    12    13.9
B    51    55.4
C    20    22.1
D    89    96.8

I want to read them and to populate an array for each column, in order to get:

array LETTERS = [A, B, C, D]
array FIRST = [12, 51, 20, 89]
array SECOND = [13.9, 55.4, 22.1, 96.8]

How can I do that in Python?

2
  • Have you tried out any code? Commented Oct 14, 2015 at 9:49
  • 1
    Please don't use all upper-case for normal variables. In Python such names are normally used for constants. Commented Oct 14, 2015 at 10:24

4 Answers 4

5
f = open('file.txt', 'r')
letters = []
first = []
second = []

for line in f:
    columns = line.split()
    letters.append(columns[0])
    first.append(columns[1])
    second.append(columns[2])
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, that was exactly what I was looking for! The last two arrays are "first" and "second" though! Thanks
1
with open('FILE.txt','r') as f:
    Letters = []
    First = []
    Second = []
    for line in f.readlines():
        Letters.append(line.split()[0])
        First.append(line.split()[1])
        Second.append(line.split()[2])

Or, alternatively:

with open('FILE.txt', 'r') as f:
    data = f.readlines()
    Letters = [line.split()[0] for line in data]
    First = [line.split()[1] for line in data]
    Second = [line.split()[2] for line in data]

Comments

0

Take into consideration your types:

with open('data.txt', 'r') as d:
    content = d.readlines()

LETTERS = []
FIRST = []
SECOND = []

for el in content:
    cur_line = el.split()
    for i, l in enumerate([(LETTERS, str), (FIRST, int), (SECOND, float)]):
        l[0].append(l[1](cur_line[i]))

print LETTERS
print FIRST
print SECOND

>>> ['A', 'B', 'C', 'D']
>>> [12, 51, 20, 89]
>>> [13.9, 55.4, 22.1, 96.8]

Comments

0

The simplest way to do this is to read the whole file into memory and use Python's convenient list manipulation facilities to create the columns.

with open(fname) as f:
    data = f.readlines()

data = [line.split() for line in data]
letters, first, second = zip(*data)

print(letters)
print(first)
print(second)

output

('A', 'B', 'C', 'D')
('12', '51', '20', '89')
('13.9', '55.4', '22.1', '96.8')

In zip(*data) the *data tells the zip function to get its arguments from the iterables in data, so zip(*data) creates a list of tuples from the successive elements of each of the lists in data. (If those iterables are unequal in length then zip stops when the shortest iterable is exhausted).

The core of the above code can be condensed to:

with open(fname) as f:
    letters, first, second = zip(*[line.split() for line in f.readlines()])

although you may find the original version more readable.

If for some reason you need lists instead of tuples, replace the zip(*data) with

[list(t) for t in zip(*data)]

However, you should probably use tuples instead of lists here, unless you need to modify them: tuples are immutable but they are slightly more efficient than lists.

If you need to do arithmetic on the values in first and second (and that includes numeric comparisons) you will need to convert them to numeric types, eg

first = [int(s) for s in first]
second = [float(s) for s in second]

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.