so I have an assignment that requires me to open a file that holds a handful of quotes, too many to analyze by hand, and another file that holds possible words that could be in the quotes. If said words end up in the quotes, I'm supposed to acknowledge it in my code, but I don't see how I'm supposed to relate one file to another... Thanks
Asked
Active
Viewed 27 times
1 Answers
0
If you wanted to compare file contents, you would need to open()
and then read()
both files. By the sounds of it, as you need to check for differences in a file, you would want to convert the texts to lists [readlines()
] and create a loop that checks for your condition.
This is how you would open your files for reading.
quotes = open("quotes.txt", "r").readlines()
words = open("words.txt", "r").readlines()
for quote in quotes:
# check if your quote contains a value in our 'words' list
I won't tell you much more, otherwise it wouldn't be an assignment. I hope this helps you get on the right track though!
P.S: How to check if a string contains an element from a list in Python

c00kie
- 306
- 1
- 6