Python - How do I print always the word from a list found in a document to another list?
up vote
1
down vote
favorite
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']
python list
add a comment |
up vote
1
down vote
favorite
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']
python list
1
When you dol_words.append(word)
, Python does not know whatword
is supposed to be because you never told it. Generator comprehensions don't leak their variables. We can't really say more without a Minimal, Complete, and Verifiable example.
– timgeb
Nov 10 at 21:30
This has nothing to do with your title, you're not trying toprint
and you haven't really explained what this is supposed to do
– roganjosh
Nov 10 at 21:31
please provide an example input and the expected output
– Ayxan
Nov 10 at 21:33
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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']
python list
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']
python list
python list
edited Nov 10 at 21:51
asked Nov 10 at 21:27
Mady
1077
1077
1
When you dol_words.append(word)
, Python does not know whatword
is supposed to be because you never told it. Generator comprehensions don't leak their variables. We can't really say more without a Minimal, Complete, and Verifiable example.
– timgeb
Nov 10 at 21:30
This has nothing to do with your title, you're not trying toprint
and you haven't really explained what this is supposed to do
– roganjosh
Nov 10 at 21:31
please provide an example input and the expected output
– Ayxan
Nov 10 at 21:33
add a comment |
1
When you dol_words.append(word)
, Python does not know whatword
is supposed to be because you never told it. Generator comprehensions don't leak their variables. We can't really say more without a Minimal, Complete, and Verifiable example.
– timgeb
Nov 10 at 21:30
This has nothing to do with your title, you're not trying toprint
and you haven't really explained what this is supposed to do
– roganjosh
Nov 10 at 21:31
please provide an example input and the expected output
– Ayxan
Nov 10 at 21:33
1
1
When you do
l_words.append(word)
, Python does not know what word
is supposed to be because you never told it. Generator comprehensions don't leak their variables. We can't really say more without a Minimal, Complete, and Verifiable example.– timgeb
Nov 10 at 21:30
When you do
l_words.append(word)
, Python does not know what word
is supposed to be because you never told it. Generator comprehensions don't leak their variables. We can't really say more without a Minimal, Complete, and Verifiable example.– timgeb
Nov 10 at 21:30
This has nothing to do with your title, you're not trying to
print
and you haven't really explained what this is supposed to do– roganjosh
Nov 10 at 21:31
This has nothing to do with your title, you're not trying to
print
and you haven't really explained what this is supposed to do– roganjosh
Nov 10 at 21:31
please provide an example input and the expected output
– Ayxan
Nov 10 at 21:33
please provide an example input and the expected output
– Ayxan
Nov 10 at 21:33
add a comment |
3 Answers
3
active
oldest
votes
up vote
0
down vote
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)
add a comment |
up vote
0
down vote
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])
add a comment |
up vote
0
down vote
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)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243580%2fpython-how-do-i-print-always-the-word-from-a-list-found-in-a-document-to-anoth%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
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)
add a comment |
up vote
0
down vote
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)
add a comment |
up vote
0
down vote
up vote
0
down vote
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)
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)
answered Nov 10 at 21:39
Paritosh Singh
72612
72612
add a comment |
add a comment |
up vote
0
down vote
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])
add a comment |
up vote
0
down vote
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])
add a comment |
up vote
0
down vote
up vote
0
down vote
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])
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])
answered Nov 10 at 21:40
roeen30
43629
43629
add a comment |
add a comment |
up vote
0
down vote
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)
add a comment |
up vote
0
down vote
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)
add a comment |
up vote
0
down vote
up vote
0
down vote
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)
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)
answered Nov 10 at 21:59
Susanna Ventafridda
1614
1614
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243580%2fpython-how-do-i-print-always-the-word-from-a-list-found-in-a-document-to-anoth%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
When you do
l_words.append(word)
, Python does not know whatword
is supposed to be because you never told it. Generator comprehensions don't leak their variables. We can't really say more without a Minimal, Complete, and Verifiable example.– timgeb
Nov 10 at 21:30
This has nothing to do with your title, you're not trying to
print
and you haven't really explained what this is supposed to do– roganjosh
Nov 10 at 21:31
please provide an example input and the expected output
– Ayxan
Nov 10 at 21:33