我正在编写一些简单的代码来逐行读取并拆分行。我可以创建一个变量来将每一行保存为列表。然后我可以打印所述列表的每个索引,但我无法将索引分配给新变量。下面是示例代码。
fileList = [ f for f in os.listdir(outputDir) if f.endswith(".txt") ]
for dayFile in fileList:
print dayFile
with open(outputDir+dayFile) as openDayFile:
for line in openDayFile.readlines():
print line
如果我只打印我得到的那条线。
20.20 -100.36 0.60
26.98 -102.06 0.00
19.36 -90.72 0.00
16.65 -95.93 0.00
如果我加入。
Three = line.split()
print Three
给我。
['20.20', '-100.36', '0.60']
['26.98', '-102.06', '0.00']
['19.36', '-90.72', '0.00'] etc...
现在我分配
A= Three[0] #there is no error.
当我指定
时A = Three[1]
或A =三[2]
我收到了错误。
IndexError: list index out of range
如果我只是做
print Three[1]
打印......
-100.36
-102.06
-90.72
-95.93
-99.12
-97.93
-96.72
-96.10
-100.93
-98.14
有人可以帮我理解问题所在吗?