Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing text in python

Tags:

python

parsing

I've got a text file that is structured like so

1\t        13249\n

2\t        3249\n

3\t        43254\n

etc...

It's a very simple list. I've got the file opened and I can read the lines. I have the following code:

count = 0
for x in open(filename):
    count += 1
return count

What I want to do is to assign the first number of each line to a variable (say xi) and to assign the second number of each line to another variable (yi). The goal is to be able to run some statistics on these numbers.

Many thanks in advance.

like image 304
Darakian Avatar asked Jul 01 '26 03:07

Darakian


1 Answers

No need to reinvent the wheel..

import numpy as np

for xi, yi in np.loadtxt('blah.txt'):
    print(xi)
    print(yi)
like image 182
wim Avatar answered Jul 03 '26 16:07

wim