In Python, how to get whole lists from a .txt file with separated spaces and "\ r \ n" numbers on several lines?

The number of lines is known at the very beginning.

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 am now:

  • Splitting the file '\ r \ n' on each line
  • Getting each line using .split () before divide by spaces and convert int (i) to integers

The 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 
+7
python
source share
3 answers

str.split() already removes spaces from the end, including a new line. No need to remove \r ; Python has already translated the line separator only to \n .

Do not try to assign multiple variables line* ; just use the list:

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

You now have a list of lists with integers.

You can simply process each line when you read it from a file; while moving to the final product, rather than holding all the 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. 
+6
source share

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 are using python3, you need to use list(map... like map return and iterator in python3 compared to list in python2.

You can also use dict to access each list by name / key, but you can use indexing so the dict will be pointless.

+5
source share

If your requirements allow you to use NumPy , you can use the loadtxt() function to read and parse the file contents in 2D Numpy ndarray . This can be useful if you need to process several lines at once, in some complex way that cannot be implemented using an iterator.

Since you mentioned that numbers do not start from the very beginning at the beginning of the file, you can use the skiprows parameter for loadtxt() to skip the first line. Or you can open the file yourself, read seven bytes from the file object, and then pass it to loadtxt() .

+1
source share

All Articles