读取txt文件时出现索引错误
Index error when reading txt file
我是 python 的新手,正在使用 Jupityr 笔记本在 edx 上做一些基本的编码作业。我在我的一项作业中遇到了一个我不太明白的索引错误。作业让我在 while 循环中一次读取一行文本文件,并以特定格式打印输出。我的代码如下
city_temp = mean_temp.readline().strip().split(',')
while city_temp:
print (headings[0].title(), "of", city_temp[0], headings[2], "is", city_temp[2], "Celsius")
city_temp = mean_temp.readline().strip().split(',')
代码 运行 遍历整个文件,但它并没有在空行处结束 'while' 循环,而是继续 运行 并创建一个空列表。我不确定为什么会这样,也无法自行解决。我试过为一个空字符串添加一个 'if' 测试并中断,并且还写了一行额外的空文本,但是这两个选项都没有成功。如果有人有想法,我将不胜感激!
我还在下面粘贴了 txt 文件包含的内容的摘录。还有其他城市,但我认为没有必要包括所有城市:
city,country,month ave: highest high,month ave: lowest low
Beijing,China,30.9,-8.4
这是我遇到的索引错误:(抱歉格式不正确,仍在学习中
IndexError Traceback (most recent call last)
<ipython-input-18-6ea7e8e263b5> in <module>()
5 while city_temp:
6
----> 7 print (headings[0].title(), "of", city_temp[0], headings[2], "is", city_temp[2], "Celsius")
8 city_temp = mean_temp.readline().strip().split(',')
9
IndexError: list index out of range
我认为您需要检查空列表而不是空字符串
city_temp = mean_temp.readline().strip().split(',')
while city_temp:
print (headings[0].title(), "of", city_temp[0], headings[2], "is", city_temp[2], "Celsius")
city_temp = mean_temp.readline().strip().split(',')
if len(city_temp) == 0:
break
一个更好的(更像 pythonic 的?)方法来完成这个是:
for line in mean_temp:
city_temp = line.strip().split(',')
try:
print ("{} of {} {} is {} Celsius".format(headings[0].title(),
city_temp[0],
headings[2],
city_temp[2]))
except IndexError:
break
您可以使用文件对象作为迭代器逐行读取文件。这个不需要 try 块,因为当它到达空行时循环将优雅地结束。另外,将此代码放在 with 语句中:
with open("myfile.txt", 'r') as mean_temp:
确保文件在您读完后关闭。
查看:https://docs.python.org/3/tutorial/inputoutput.html
澄清问题中实际发生的事情:
当 readline() 到达文件末尾时,它 returns 一个空字符串。当您在空字符串上使用 strip() 时,它 returns 是一个空列表。当您在空字符串上使用 strip(',') 或任何分隔符时,它 returns 一个包含空字符串的列表。在这种情况下,您的 while 循环正在检查列表。由于列表不为空,它返回 True 并且 while 循环继续。如果您需要 while 循环,我的建议是:
line = mean_temp.readline()
while line:
city_temp = line.strip().split(',')
print ("{} of {} {} is {} Celsius".format(headings[0].title(),
city_temp[0],
headings[2],
city_temp[2]))
line = mean_temp.readline()
这可能是 while 逐行循环遍历文件的最干净的方法。几乎与上面的 for 循环完全相同。
我是 python 的新手,正在使用 Jupityr 笔记本在 edx 上做一些基本的编码作业。我在我的一项作业中遇到了一个我不太明白的索引错误。作业让我在 while 循环中一次读取一行文本文件,并以特定格式打印输出。我的代码如下
city_temp = mean_temp.readline().strip().split(',')
while city_temp:
print (headings[0].title(), "of", city_temp[0], headings[2], "is", city_temp[2], "Celsius")
city_temp = mean_temp.readline().strip().split(',')
代码 运行 遍历整个文件,但它并没有在空行处结束 'while' 循环,而是继续 运行 并创建一个空列表。我不确定为什么会这样,也无法自行解决。我试过为一个空字符串添加一个 'if' 测试并中断,并且还写了一行额外的空文本,但是这两个选项都没有成功。如果有人有想法,我将不胜感激!
我还在下面粘贴了 txt 文件包含的内容的摘录。还有其他城市,但我认为没有必要包括所有城市:
city,country,month ave: highest high,month ave: lowest low
Beijing,China,30.9,-8.4
这是我遇到的索引错误:(抱歉格式不正确,仍在学习中
IndexError Traceback (most recent call last)
<ipython-input-18-6ea7e8e263b5> in <module>()
5 while city_temp:
6
----> 7 print (headings[0].title(), "of", city_temp[0], headings[2], "is", city_temp[2], "Celsius")
8 city_temp = mean_temp.readline().strip().split(',')
9
IndexError: list index out of range
我认为您需要检查空列表而不是空字符串
city_temp = mean_temp.readline().strip().split(',')
while city_temp:
print (headings[0].title(), "of", city_temp[0], headings[2], "is", city_temp[2], "Celsius")
city_temp = mean_temp.readline().strip().split(',')
if len(city_temp) == 0:
break
一个更好的(更像 pythonic 的?)方法来完成这个是:
for line in mean_temp:
city_temp = line.strip().split(',')
try:
print ("{} of {} {} is {} Celsius".format(headings[0].title(),
city_temp[0],
headings[2],
city_temp[2]))
except IndexError:
break
您可以使用文件对象作为迭代器逐行读取文件。这个不需要 try 块,因为当它到达空行时循环将优雅地结束。另外,将此代码放在 with 语句中:
with open("myfile.txt", 'r') as mean_temp:
确保文件在您读完后关闭。
查看:https://docs.python.org/3/tutorial/inputoutput.html
澄清问题中实际发生的事情:
当 readline() 到达文件末尾时,它 returns 一个空字符串。当您在空字符串上使用 strip() 时,它 returns 是一个空列表。当您在空字符串上使用 strip(',') 或任何分隔符时,它 returns 一个包含空字符串的列表。在这种情况下,您的 while 循环正在检查列表。由于列表不为空,它返回 True 并且 while 循环继续。如果您需要 while 循环,我的建议是:
line = mean_temp.readline()
while line:
city_temp = line.strip().split(',')
print ("{} of {} {} is {} Celsius".format(headings[0].title(),
city_temp[0],
headings[2],
city_temp[2]))
line = mean_temp.readline()
这可能是 while 逐行循环遍历文件的最干净的方法。几乎与上面的 for 循环完全相同。