Reading a file into a list on python. How to take out words -
i reading file list , spliting every word in list. not want specific words brought in list, skip them. called trash list filterlist written below.
this code:
with open('usconstitution.txt') f: lines = f.read().split() #read list filterlist = ["a","an","the","as","if","and","not"] #define filterlist word in lines: if word.lower() not in filterlist: word.append(alist) #place them in new list called alist not contain in filterlist print(alist) #print new list
i getting error:
attributeerror: 'str' object has no attribute 'append'
can ? thanks
you need give,
alist.append(word)
list object has attribute append
. , need declare list first. append items list.
ie,
with open('usconstitution.txt') f: lines = f.read().split() #read list filterlist = ["a","an","the","as","if","and","not"] #define filterlist alist = [] word in lines: if word.lower() not in filterlist: alist.append(word) #place them in new list called alist not contain in filterlist print(alist)