【发布时间】:2020-05-10 02:17:28
【问题描述】:
这就是我目前为止的课程项目
import os
UserSelection = input("Select a txt file: ")
with open(UserSelection, 'r', encoding= "latin-1") as f:
lines= f.readlines()
print("These are your headers:", lines[0])
state = words()[6]
for i in range(1,len(lines)):
words=line.split
if(words()[6] == 'California'):
print(lines)
我有一个大的人口普查表,我试图只保留 STATE(即第 6 个索引)为 CA 的行。
我想
words()[6] 会这样做,但不是。
提前谢谢!
【问题讨论】:
-
应该是
words=lines[i].split(),它按空格分割并提供单词列表。您实际上可能想要使用words=lines[i].rstrip().split()删除每行末尾的回车符。索引 6 则为words[6]。
标签: python python-3.x list readlines