我有一个带有整数的巨大文本文件。我需要逐行处理它们,并根据每行中数字的一些计算将其保存在单独的列表中
最终目标是将数字(第1行除外)加载到两个列表中 - A =奇数位置的数字B =偶数位置的数字
档案样本:
1 3 4 5
3 4 56 73
3 4 5 6
目前我正在做:
with open(filename) as f:
for line in f:
line = line.split()
line_num = line_num + 1
if line_num == 1:
# do something
pass
if line_num > 1:
line = [int(i) for i in line]
for x in range(len(line)):
# do something
pass
问题是,它花了很多时间。有没有更好的方法来快速完成这项工作?
答案 0 :(得分:1)
对于numpy来说听起来很有效:
X = numpy.loadtxt(filename) #can specify if you know for sure all are integers
odds = X[1::2]
evens = X[::2]
答案 1 :(得分:0)
不是每次检查线是否是第一行,而是在开头处理第一行。无需检查循环内部。
with open(filename) as f:
line = next(f)
# do something for the first line
# handle rest lines
for line in f:
line = line.split()
line = [int(i) for i in line]
for field in line:
# do something with field
pass
我删除了line_num
,因为在orignal代码中没有用。但如果您需要,请使用enumerate
:
with open(filename) as f:
line = next(f)
for line_num, line in enumerate(f, 2):
...