Trying to save keywords from a .txt file to an array and use that array to search another doc for the...












0















I am trying to save a bunch of keywords in a file called keywordtest.txt containing words such as networkmanager, filemanager and speaker etc. (Each on its own line in the .txt file).



I have got it to save the words into an array but I then want it to use the words stored in that array to search in another document called test and bring up the whole line that the keywords was found in and store that line in another doc called results.txt.



I have managed to get it to search using the words in the array and save them to another file but will only find something if it is not contained in a line:



For example, I search for networkmanager and if it is just networkmanager on a line of its own in the file I am searching in it will find it, but if its like 'I am a networkmanager' for example it wont pick it up.



#!usrbinpython

x =

with open("keywordtest.txt","r") as y:
for line in y:
x.append(line)
print x

theFile = open ("results.txt", "w")
searchfile = open("test", "r")

for line in searchfile:
for word in x:
if word in line:
theFile.write(line)

theFile.close()
searchfile.close()


EDIT: the code i posted was a version of me when trying something out, i seen online someone using 'word' instead of 'line' in some places and tried it out but it didnt like it, the code below is the code i have got to work but only displays the ekywords i am searching for if they are on their own.



#!usrbinpython

x =

with open("keywordtest.txt","r") as y:
for line in y:
x.append(line)
print x


theFile = open ("results.txt", "w")
searchfile = open("test", "r")

for line in searchfile:
if line in x: theFile.write(line)
if line in x: print line

theFile.close()
searchfile.close()









share|improve this question

























  • at last you want results.txt file with the matched words ???

    – Himanshu dua
    Apr 23 '15 at 13:39
















0















I am trying to save a bunch of keywords in a file called keywordtest.txt containing words such as networkmanager, filemanager and speaker etc. (Each on its own line in the .txt file).



I have got it to save the words into an array but I then want it to use the words stored in that array to search in another document called test and bring up the whole line that the keywords was found in and store that line in another doc called results.txt.



I have managed to get it to search using the words in the array and save them to another file but will only find something if it is not contained in a line:



For example, I search for networkmanager and if it is just networkmanager on a line of its own in the file I am searching in it will find it, but if its like 'I am a networkmanager' for example it wont pick it up.



#!usrbinpython

x =

with open("keywordtest.txt","r") as y:
for line in y:
x.append(line)
print x

theFile = open ("results.txt", "w")
searchfile = open("test", "r")

for line in searchfile:
for word in x:
if word in line:
theFile.write(line)

theFile.close()
searchfile.close()


EDIT: the code i posted was a version of me when trying something out, i seen online someone using 'word' instead of 'line' in some places and tried it out but it didnt like it, the code below is the code i have got to work but only displays the ekywords i am searching for if they are on their own.



#!usrbinpython

x =

with open("keywordtest.txt","r") as y:
for line in y:
x.append(line)
print x


theFile = open ("results.txt", "w")
searchfile = open("test", "r")

for line in searchfile:
if line in x: theFile.write(line)
if line in x: print line

theFile.close()
searchfile.close()









share|improve this question

























  • at last you want results.txt file with the matched words ???

    – Himanshu dua
    Apr 23 '15 at 13:39














0












0








0








I am trying to save a bunch of keywords in a file called keywordtest.txt containing words such as networkmanager, filemanager and speaker etc. (Each on its own line in the .txt file).



I have got it to save the words into an array but I then want it to use the words stored in that array to search in another document called test and bring up the whole line that the keywords was found in and store that line in another doc called results.txt.



I have managed to get it to search using the words in the array and save them to another file but will only find something if it is not contained in a line:



For example, I search for networkmanager and if it is just networkmanager on a line of its own in the file I am searching in it will find it, but if its like 'I am a networkmanager' for example it wont pick it up.



#!usrbinpython

x =

with open("keywordtest.txt","r") as y:
for line in y:
x.append(line)
print x

theFile = open ("results.txt", "w")
searchfile = open("test", "r")

for line in searchfile:
for word in x:
if word in line:
theFile.write(line)

theFile.close()
searchfile.close()


EDIT: the code i posted was a version of me when trying something out, i seen online someone using 'word' instead of 'line' in some places and tried it out but it didnt like it, the code below is the code i have got to work but only displays the ekywords i am searching for if they are on their own.



#!usrbinpython

x =

with open("keywordtest.txt","r") as y:
for line in y:
x.append(line)
print x


theFile = open ("results.txt", "w")
searchfile = open("test", "r")

for line in searchfile:
if line in x: theFile.write(line)
if line in x: print line

theFile.close()
searchfile.close()









share|improve this question
















I am trying to save a bunch of keywords in a file called keywordtest.txt containing words such as networkmanager, filemanager and speaker etc. (Each on its own line in the .txt file).



I have got it to save the words into an array but I then want it to use the words stored in that array to search in another document called test and bring up the whole line that the keywords was found in and store that line in another doc called results.txt.



I have managed to get it to search using the words in the array and save them to another file but will only find something if it is not contained in a line:



For example, I search for networkmanager and if it is just networkmanager on a line of its own in the file I am searching in it will find it, but if its like 'I am a networkmanager' for example it wont pick it up.



#!usrbinpython

x =

with open("keywordtest.txt","r") as y:
for line in y:
x.append(line)
print x

theFile = open ("results.txt", "w")
searchfile = open("test", "r")

for line in searchfile:
for word in x:
if word in line:
theFile.write(line)

theFile.close()
searchfile.close()


EDIT: the code i posted was a version of me when trying something out, i seen online someone using 'word' instead of 'line' in some places and tried it out but it didnt like it, the code below is the code i have got to work but only displays the ekywords i am searching for if they are on their own.



#!usrbinpython

x =

with open("keywordtest.txt","r") as y:
for line in y:
x.append(line)
print x


theFile = open ("results.txt", "w")
searchfile = open("test", "r")

for line in searchfile:
if line in x: theFile.write(line)
if line in x: print line

theFile.close()
searchfile.close()






python arrays loops python-3.x arraylist






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 19 at 20:29









Flimzy

38.3k96597




38.3k96597










asked Apr 23 '15 at 13:32









JamesB123JamesB123

13




13













  • at last you want results.txt file with the matched words ???

    – Himanshu dua
    Apr 23 '15 at 13:39



















  • at last you want results.txt file with the matched words ???

    – Himanshu dua
    Apr 23 '15 at 13:39

















at last you want results.txt file with the matched words ???

– Himanshu dua
Apr 23 '15 at 13:39





at last you want results.txt file with the matched words ???

– Himanshu dua
Apr 23 '15 at 13:39












2 Answers
2






active

oldest

votes


















0














    #!usrbinpython

x =

with open("keywordtest.txt","r") as y:
for line in y:
x.append(line)
print x

theFile = open ("results.txt", "w")
searchfile = open("test.txt", "r")

for line in searchfile:
for word in x:
if word in line.strip():
theFile.write(word)

theFile.close()
searchfile.close()





share|improve this answer
























  • sorry guys made a mistake with the code i posted, it was an older trial and error version that wasn't working. If you could take a look at the code i have now put as it works when searching for a word one its own but wont find it if that would is in a line. sorry and thanks for your help

    – JamesB123
    Apr 23 '15 at 13:54



















0














based on your edit:



#!usrbinpython
x =

with open("keywordtest.txt","r") as y:
for line in y:
x.append(line)
print x


theFile = open ("results.txt", "w")
searchfile = open("test.txt", "r")

for line in searchfile:
for item in x:
if item in line:
theFile.write(line)
print line

theFile.close()
searchfile.close()


This saves the matched words in theFile.txt and will print out the matches assuming keywordtest.txt has only one word per line same as test.txt






share|improve this answer
























  • this has pulled up everything in my test file even if its not one of my keywords contained in the array

    – JamesB123
    Apr 24 '15 at 8:43











  • @JamesB123 can you give an example of what these files contain?

    – bladexeon
    Apr 24 '15 at 14:40











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',
autoActivateHeartbeat: false,
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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f29825044%2ftrying-to-save-keywords-from-a-txt-file-to-an-array-and-use-that-array-to-searc%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














    #!usrbinpython

x =

with open("keywordtest.txt","r") as y:
for line in y:
x.append(line)
print x

theFile = open ("results.txt", "w")
searchfile = open("test.txt", "r")

for line in searchfile:
for word in x:
if word in line.strip():
theFile.write(word)

theFile.close()
searchfile.close()





share|improve this answer
























  • sorry guys made a mistake with the code i posted, it was an older trial and error version that wasn't working. If you could take a look at the code i have now put as it works when searching for a word one its own but wont find it if that would is in a line. sorry and thanks for your help

    – JamesB123
    Apr 23 '15 at 13:54
















0














    #!usrbinpython

x =

with open("keywordtest.txt","r") as y:
for line in y:
x.append(line)
print x

theFile = open ("results.txt", "w")
searchfile = open("test.txt", "r")

for line in searchfile:
for word in x:
if word in line.strip():
theFile.write(word)

theFile.close()
searchfile.close()





share|improve this answer
























  • sorry guys made a mistake with the code i posted, it was an older trial and error version that wasn't working. If you could take a look at the code i have now put as it works when searching for a word one its own but wont find it if that would is in a line. sorry and thanks for your help

    – JamesB123
    Apr 23 '15 at 13:54














0












0








0







    #!usrbinpython

x =

with open("keywordtest.txt","r") as y:
for line in y:
x.append(line)
print x

theFile = open ("results.txt", "w")
searchfile = open("test.txt", "r")

for line in searchfile:
for word in x:
if word in line.strip():
theFile.write(word)

theFile.close()
searchfile.close()





share|improve this answer













    #!usrbinpython

x =

with open("keywordtest.txt","r") as y:
for line in y:
x.append(line)
print x

theFile = open ("results.txt", "w")
searchfile = open("test.txt", "r")

for line in searchfile:
for word in x:
if word in line.strip():
theFile.write(word)

theFile.close()
searchfile.close()






share|improve this answer












share|improve this answer



share|improve this answer










answered Apr 23 '15 at 13:40









Himanshu duaHimanshu dua

1,477721




1,477721













  • sorry guys made a mistake with the code i posted, it was an older trial and error version that wasn't working. If you could take a look at the code i have now put as it works when searching for a word one its own but wont find it if that would is in a line. sorry and thanks for your help

    – JamesB123
    Apr 23 '15 at 13:54



















  • sorry guys made a mistake with the code i posted, it was an older trial and error version that wasn't working. If you could take a look at the code i have now put as it works when searching for a word one its own but wont find it if that would is in a line. sorry and thanks for your help

    – JamesB123
    Apr 23 '15 at 13:54

















sorry guys made a mistake with the code i posted, it was an older trial and error version that wasn't working. If you could take a look at the code i have now put as it works when searching for a word one its own but wont find it if that would is in a line. sorry and thanks for your help

– JamesB123
Apr 23 '15 at 13:54





sorry guys made a mistake with the code i posted, it was an older trial and error version that wasn't working. If you could take a look at the code i have now put as it works when searching for a word one its own but wont find it if that would is in a line. sorry and thanks for your help

– JamesB123
Apr 23 '15 at 13:54













0














based on your edit:



#!usrbinpython
x =

with open("keywordtest.txt","r") as y:
for line in y:
x.append(line)
print x


theFile = open ("results.txt", "w")
searchfile = open("test.txt", "r")

for line in searchfile:
for item in x:
if item in line:
theFile.write(line)
print line

theFile.close()
searchfile.close()


This saves the matched words in theFile.txt and will print out the matches assuming keywordtest.txt has only one word per line same as test.txt






share|improve this answer
























  • this has pulled up everything in my test file even if its not one of my keywords contained in the array

    – JamesB123
    Apr 24 '15 at 8:43











  • @JamesB123 can you give an example of what these files contain?

    – bladexeon
    Apr 24 '15 at 14:40
















0














based on your edit:



#!usrbinpython
x =

with open("keywordtest.txt","r") as y:
for line in y:
x.append(line)
print x


theFile = open ("results.txt", "w")
searchfile = open("test.txt", "r")

for line in searchfile:
for item in x:
if item in line:
theFile.write(line)
print line

theFile.close()
searchfile.close()


This saves the matched words in theFile.txt and will print out the matches assuming keywordtest.txt has only one word per line same as test.txt






share|improve this answer
























  • this has pulled up everything in my test file even if its not one of my keywords contained in the array

    – JamesB123
    Apr 24 '15 at 8:43











  • @JamesB123 can you give an example of what these files contain?

    – bladexeon
    Apr 24 '15 at 14:40














0












0








0







based on your edit:



#!usrbinpython
x =

with open("keywordtest.txt","r") as y:
for line in y:
x.append(line)
print x


theFile = open ("results.txt", "w")
searchfile = open("test.txt", "r")

for line in searchfile:
for item in x:
if item in line:
theFile.write(line)
print line

theFile.close()
searchfile.close()


This saves the matched words in theFile.txt and will print out the matches assuming keywordtest.txt has only one word per line same as test.txt






share|improve this answer













based on your edit:



#!usrbinpython
x =

with open("keywordtest.txt","r") as y:
for line in y:
x.append(line)
print x


theFile = open ("results.txt", "w")
searchfile = open("test.txt", "r")

for line in searchfile:
for item in x:
if item in line:
theFile.write(line)
print line

theFile.close()
searchfile.close()


This saves the matched words in theFile.txt and will print out the matches assuming keywordtest.txt has only one word per line same as test.txt







share|improve this answer












share|improve this answer



share|improve this answer










answered Apr 23 '15 at 14:11









bladexeonbladexeon

352424




352424













  • this has pulled up everything in my test file even if its not one of my keywords contained in the array

    – JamesB123
    Apr 24 '15 at 8:43











  • @JamesB123 can you give an example of what these files contain?

    – bladexeon
    Apr 24 '15 at 14:40



















  • this has pulled up everything in my test file even if its not one of my keywords contained in the array

    – JamesB123
    Apr 24 '15 at 8:43











  • @JamesB123 can you give an example of what these files contain?

    – bladexeon
    Apr 24 '15 at 14:40

















this has pulled up everything in my test file even if its not one of my keywords contained in the array

– JamesB123
Apr 24 '15 at 8:43





this has pulled up everything in my test file even if its not one of my keywords contained in the array

– JamesB123
Apr 24 '15 at 8:43













@JamesB123 can you give an example of what these files contain?

– bladexeon
Apr 24 '15 at 14:40





@JamesB123 can you give an example of what these files contain?

– bladexeon
Apr 24 '15 at 14:40


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f29825044%2ftrying-to-save-keywords-from-a-txt-file-to-an-array-and-use-that-array-to-searc%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Homophylophilia

Updating UILabel text programmatically using a function

Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage