Python - reading numbers from a text file and listing

So, as the name says, I'm starting to learn some pythons, and they fail to understand this technique. What I need to do is read in some numbers and save them in a list. The text file is as follows:

0 0 3 50 50 100 4 20 

These are basically the coordinates and directions used for the python tortoise to create shapes. I got this part, the only problem is getting them in the correct format. Therefore, I can’t understand how to get these numbers from a file in the [ [0, 0, 3, 50], [50, 100, 4, 20] ] list, each of the four coordinates of which is a list in this large list.

Here is my attempt, but, as I said, I need help - thanks.

 polyShape=[] infile = open(name,"r") num = int(infile.readline(2)) while num != "": polyShape.append(num) num = int(infile.readline(2)) infile.close() 
+4
source share
6 answers
 with open('data.txt') as f: polyShape = [] for line in f: line = line.split() # to deal with blank if line: # lines (ie skip them) line = [int(i) for i in line] polyShape.append(line) 

will provide you

 [[0, 0, 3, 50], [50, 100, 4, 20]] 

This will work with a file that contains empty lines (or not).

Using the with construct will automatically close the file if you are finished or an exception is thrown.

+11
source

Assuming there is no empty line in your input file:

 with open(name, "r") as infile: polyShape = [map(int, line.split()) for line in infile] 

Explanation: map(int, line.split()) splits each line and converts each part to int . The [X for Y in Z] construct is an understanding of the list, which, in turn, displays map along all lines of the file and returns its results in the list.

If you find this too complicated at the moment, then map(int, line.split()) is the main return message.

+3
source
 with open('data.txt') as f: lis=[map(int,x.split()) for x in f if x.strip()] # if x.strip() to skip blank lines #use list(map(int,x.split())) in case of python 3.x 

This is how map() works:

 >>> map(int,'1 2 3 4'.split()) [1, 2, 3, 4] 
+2
source

Iterating over a file would be the easiest way:

 poly_shape = [] with open(name, 'r') as handle: for line in handle: if not line.strip(): continue # This skips blank lines values = map(int, line.split()) poly_shape.append(values) 
+1
source

Single line:

 [ [int(x) for x in line.split(' ')] for line in open(name,'r').readlines() if line.strip()] 

but the readlines part is probably not a good idea.

I am sure that [int(x) for x in ... ] faster than using map , as in other proposed solutions.

Edit

Thanks to Blender: there is no need for .readlines , which is cool, so we just have:

 [ map(int, line.split()) for line in open(name,'r') if line.strip()] 

I also used map(int, ) because it works faster, and also you can use only line.split() instead of line.split(' ') .

+1
source

I do not recommend using append for a large array. This is 50 times slower than creating a null array and assigning values ​​to it.

 import numpy fname = "D:\Test.txt"; num_lines = sum(1 for line in open(fname)); array = numpy.zeros((num_lines,4)); k = 0; with open(fname, "r") as ins: for line in ins: a =[int(i) for i in line.split(' ')];; array[k,0:4] =a; k = k+1; print(array) 
0
source

Source: https://habr.com/ru/post/1432372/


All Articles