python - Read first N lines of a file in a 2d list. Its not same as for 1d case -
i have following type of data in text file.
15 1 23 0 39 -1 71 -1 79 1 95 1 127 -2 151 2 183 1 191 -1 239 0 247 3
i want create 2d list text file follows. able code given below following result
[[15, 1], [23, 0], [39, -1], [71, -1], [79, 1], [95, 1], [127, -2], [151, 2], [183, 1], [191, -1], [239, 0], [247, 3]]
the problem file contains millions of such data. do copy first 1000 lines @ time? how can add conditional statement code.
with open("path.text") file: r = [[int(x) x in line.split()] line in file]
here way of doing that, not sure if it's best.
with open("test2.txt") file: r = [] while len(r) < 1000: r.append([int(x) x in file.readline().split()])
Comments
Post a Comment