在python上将文件读入列表。如何取出单词

2024-03-28 23:06:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我把一个文件读入一个列表,然后把它拆分,这样每个单词都在一个列表中。但是,我不想在列表中提到具体的词,我想跳过它们。我把下面写的垃圾清单叫做过滤器清单。你知道吗

这是我的密码:

with open('USConstitution.txt') as f:
    lines = f.read().split()              #read everything into the list

filterList = ["a","an","the","as","if","and","not"]  #define a filterList

for word in lines:
    if word.lower() not in filterList:
        word.append(aList)   #place them in a new list called aList that does not contain anything in filterList


print(aList)    #print that new list

我得到这个错误:

AttributeError: 'str' object has no attribute 'append'

有人能帮忙吗?谢谢


Tags: thein列表newreadifthatas
1条回答
网友
1楼 · 发布于 2024-03-28 23:06:58

你需要付出

aList.append(word)

列表对象只有append属性。你还需要先申报名单。只有你才能把这些项目附加到列表中。你知道吗

伊恩

with open('USConstitution.txt') as f:
    lines = f.read().split()              #read everything into the list

filterList = ["a","an","the","as","if","and","not"]  #define a filterList
aList = []
for word in lines:
    if word.lower() not in filterList:
        aList.append(word)   #place them in a new list called aList that does not contain anything in filterList


print(aList) 

相关问题 更多 >