Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Python, how to get integer lists from a .txt file with space separated and '\r\n' delimited numbers on multiple lines?

Tags:

python

The number of lines is known at the outset.

Input file:

0 1 2 3 4 5 6 7 8
8 1 2 3 4 5 6 7 0
4 0 8 2 6 3 7 1 5
..n such lines

Desired result:

line1 = [0, 1, 2, 3, 4, 5, 6, 7, 8]
line2 = [8, 1, 2, 3, 4, 5, 6, 7, 0]
line3 = [4, 0, 8, 2, 6, 3, 7, 1, 5]
.
.
linen = [n1, ........           n9]

I'm currently:

  • Striping the file of the '\r\n' on every line
  • Getting each line using .split() to separate at the spaces and int(i) to convert to integers

Code:

#The lines start at the 7th byte in the input file.
f.seek(7)

#Getting rid of the '\r\n'
lines = [line.rstrip('\n\r') for line in f]

#1st line
line0 = lines[0]
line = [[int(i) for i in line0.split()]]
print line


...& so on for the 'n' lines
like image 420
Ketcomp Avatar asked Jul 12 '15 01:07

Ketcomp


2 Answers

str.split() already removes whitespace from the end, including a newline. There is no need to strip the \r; Python already has translated the line separator to just \n.

Don't try to assign to multiple line* variables; just use a list instead:

with open(filename, 'r') as fobj:
    all_lines = [[int(num) for num in line.split()] for line in fobj]

Now you have a list of lists with integers.

You could just process each line as you read it from the file; move towards the end product at that time rather than hold all lines in memory:

with open(filename, 'r') as fobj:
    for line in fobj:
        numbers = [int(num) for num in line.split()]
        # do something with this line of numbers before moving on to the next.
like image 131
Martijn Pieters Avatar answered Oct 20 '22 20:10

Martijn Pieters


Just split and map to int, split will do all the work for you:

with open("in.txt") as f:
    for line in f:
        print(map(int,line.split())) # list(map(int,line.split())) for py3

To get a list of lists use a list comprehension:

with open("in.txt") as f:
    data = [map(int,line.split()) for line in f]

If you use python3 you need to use list(map... as map returns and iterator in python3 vs a list in python2.

You could also use a dict to access each list by name/key but you can use indexing so a dict would be pointless.

like image 5
Padraic Cunningham Avatar answered Oct 20 '22 20:10

Padraic Cunningham