小编Sin*_*ton的帖子

从文本文件中读取行后,python代码中的IndexError

有人能告诉我这个python代码出了什么问题吗?看起来很傻,因为代码非常简单,但我是编码的初学者,希望可以理解.

f = open("countries.txt", "r")

countries = []

for line in f:
    line = line.strip()
    countries.append(line)

f.close()

print(countries)
print(len(countries))

for country in countries:
    if country[0] == "T":
        print(country)
Run Code Online (Sandbox Code Playgroud)

我一直收到以下错误:

 line 16, in <module>
    if country[0] == "T":
IndexError: string index out of range
Run Code Online (Sandbox Code Playgroud)

python python-3.x

1
推荐指数
1
解决办法
57
查看次数

标签 统计

python ×1

python-3.x ×1