I want to have one list with the whole line and one list with the word, so i can export it later to excel.
my code always returns:
NameError: name 'word' is not defined
Here is my code:
l_lv = []
l_words = []
fname_in = "test.txt"
fname_out = "Ergebnisse.txt"
search_list =['kostenlos', 'bauseits', 'ohne Vergütung']
with open(fname_in,'r') as f_in:
for line in f_in:
if any (word in line for word in search_list):
l_lv.append(line)
l_words.append(word)
print(l_lv)
print(l_words)
Edit: I have a file with text in it, which looks somthing like fname_in and a list of word i want it to be search by (search_list). Always when the word is found in the file i want the word to be written into the list l_words and the sentance to the list l_lv.
The code for the lines works. But it doesn't return the words.
Here an exampel:
fname_in ='sentance1 with kostenlos in it. blablabla. another sentance2 with kostenlos in it. sentance3 with bauseits in it. blablabla. another sentance4 with bauseits in it. blablabla.'
As an result i wish to have:
l_lv = ['sentance1 with kostenlos in it', 'another sentance2 with kostenlos in it','sentance3 with bauseits in it', 'another sentance4 with bauseits in it']
l_words = ['kostenlos', 'kostenlos', 'bauseits', 'bauseits']
You do not have access to variables outside a list comprehension/generator expressions and so on. The error is valid in the sense that "word" is not defined when you try to append it.
l_lv = []
l_words = []
fname_in = "test.txt"
fname_out = "Ergebnisse.txt"
search_list =['kostenlos', 'bauseits', 'ohne Vergütung']
with open(fname_in,'r') as f_in:
for line in f_in:
if any(word in line for word in search_list):
l_lv.append(line)
#for nested list instead of a flat list of words
#(to handle cases where more than 1 word matches in the same sentence.)
#words_per_line = []
for word in search_list:
l_words.append(word)
#words_per_line.append(word)
#if words_per_line:
#l_words.append(words_per_line)
print(l_lv)
print(l_words)
The variable word
is only bound in the generator expression passed to any()
, so it doesn't exist when you try to add it to a list later on. It seems that you want to know not only if a word from the search list appeared in the line but also which ones. Try this:
for line in f_in:
found = [word for word in search_list if word in line]
if found:
l_lv.append(line)
l_words.append(found)
Note that this code assumes more than one word can appear in each line, and appends a list of words to l_lv for each line, meaning that l_lv is a list of lists. If you want to append only the first word found in each line:
l_words.append(found[0])
Avoid writing for loops on one line: it looses in readability and can cause problems.
Try this:
l_lv = []
l_words = []
input_file = "test.txt"
output_file = "Ergebnisse.txt"
search_list =['kostenlos', 'bauseits', 'ohne Vergütung']
with open(input_file,'r') as f:
for line in f:
for word in search_list:
if word in line:
l_lv.append(line)
l_words.append(word)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.