0

I am creating a page scraper and would like to include a text file that contains the words such as 'a', 'the', 'on' etc. But not sure how to do this, i think I have created the file

fin=open("ignorelist.txt","w")
for line in fin:

but not sure how to go about filling the file with these words found, any help would be great.

3
Contributors
2
Replies
21
Views
3 Years
Discussion Span
Last Post by vegaseat
0

for statement iteration is only for reading, for writing it is recommended to use with statement for safe opening and closing of the file.

with open("ignorelist.txt", "w") as fout:
    fout.write('\n'.join(my_ignorelist))
0

This might help too ...

# words you want to ignore later
text = '''\
a
the
on
up
down
'''

fname = "ignorelist.txt" 
# write the file out
with open(fname, "w") as fout:
    fout.write(text)

# read the file back in and convert into a list
with open(fname, "r") as fin:
    ignorelist = [ word.strip() for word in fin]

print(ignorelist)

''' result ...
['a', 'the', 'on', 'up', 'down']
'''
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.