Printing results to text file - python

I am wanting to print the results shown in my console from the loop below into a text file. I have tried putting this code in the loop as seen in the example:
f = open('out.txt', 'w',)
sys.stdout = f
However when this is in the loop I only get one set of results instead of the full expected.
wordlist = input("What is your word list called?")
f = open(wordlist)
l = set(w.strip().lower() for w in f)
chatlog = input("What is your chat log called?")
with open(chatlog) as f:
found = False
for line in f:
line = line.lower()
if any(w in line for w in l):
print (l)
print(line)
found = True
f = open('out.txt', 'w',)
sys.stdout = f
if not found:
print("not here")

You should use write() function to write your result into the file.
Code should be something as:
wordlist = input("What is your word list called?")
f = open(wordlist)
l = set(w.strip().lower() for w in f)
chatlog = input("What is your chat log called?")
with open(chatlog) as f:
found = False
file = open("out.txt", "w")
for line in f:
line = line.lower()
if any(w in line for w in l):
found = True
file.write(line)
if not found:
print("not here")

You should make sure that you open the 'out.txt' for writing outside the loop, not inside the loop

If you want to write text to a file, you should use the .write() method of the File object, as specified in https://docs.python.org/2/tutorial/inputoutput.html, rather than the print method.

The problem is, each iteration you open it with write mode
Due to python documentation:
'w' for writing (truncating the file if it already exists)
for your purposes you should use "a" mode (append) or open file out of cycle

Related

How to check if a word is in a list that is in another function?

To put it simply, I was given a .txt file with a list of words in it, then asked to read it in python and store it in a list with a function. So far, no problem. I wrote something that looks like this:
def load_lexicon():
with open("lexicon.txt", "r") as f:
content_list = f.read().splitlines()
lexique = open("lexicon.txt", "r")
liste_des_mots = []
for i in lexique:
ligne = i.strip()
liste = ligne.split()
liste_des_mots.append(liste)
return liste_des_mots
The problem is that I would like to create a function that accesses this list that I created in the previous function and checks if a given word is there or not.
I tried something like this:
def check_word():
search_word = input("enter the word you want to search for: ")
if search_word in load_lexicon():
print ('word found')
else:
print ('word not found')
But the thing is, no matter what word I put in, whether it's in the .txt file or not it always returns that the word is not in it, for example:
enter the word you want to search for: 'a'
word not found
or
enter the word you want to search for: a
word not found
just so you know, a is in the .txt file
First you open the file and read the contents as content_list then you make another file handle called lexique. So you never actually do anything with the file contents. Additionally as #quamrana mentions you would need to either iterate through each word on each line or use extends to add them to your list otherwise that list is a list of lines which is ironically exactly what content_list was before that for loop.
def load_lexicon():
with open("lexicon.txt", "r") as f:
content_list = f.readlines()
liste_des_mots = []
for i in content_list:
ligne = i.strip()
liste = ligne.split()
liste_des_mots.extend(liste)
return liste_des_mots
def load_lexicon():
with open("lexicon.txt", "r") as f:
content_list = f.read().splitlines()
global liste_des_mots
liste_des_mots = []
lexique = open("lexicon.txt", "r")
for i in lexique:
ligne = i.strip()
#print(ligne)
liste = ligne.split()
liste_des_mots.append(liste)
return liste_des_mots
#print(liste_des_mots)
count =len(liste_des_mots)
def check_word():
search_word = input("enter the word you want to search for: ")
for i in range(count):
#print(liste_des_mots[i])
if search_word in liste_des_mots[i]:
print('word found')
break
else:
print('word not found')
check_word()
in your code you must focus on this line : if search_word in load_lexicon():
This is most likely a nested list. For this reason, it cannot make the call correctly. Once again a iteration is required within the list.
I tried to fix it. I hope it helps

Checking if an item is in a set in Python?

Writing a program to check if a word that the user inputs is in a pre-existing set; no matter what word I input the program returns "False" (even when it's a word I know is in the set). Code is below:
name = input("which file would you like to open? ")
s=input("word? ")
F = open(name, "r")
words = set()
words.add(F)
def contains(s,words):
if s in words:
print("true")
else:
print("false")
contains(s,words)
Assuming there is one word per line in the file, e.g.
asd
asdf
You can use this, which adds every line to words:
name = input("which file would you like to open? ")
s = input("word? ")
F = open(name, "r")
words = set()
for line in F: # add every line to words (assuming one word per line)
words.add(line.strip())
def contains(s, words):
if s in words:
print("true")
else:
print("false")
contains(s, words)
Printing an output of:
which file would you like to open? file.txt
word? asd
true
Edit: a much shorter way for the actual task:
name = input("which file would you like to open? ")
s = input("word? ")
F = open(name, "r")
print("true") if s in F.read() else print("false")
Assuming your file looks like this:
banana
apple
apple
orange
Let's create that file:
with open("test.txt","w") as f:
f.write("banana\napple\napple\norange")
Now let's run a sample code:
s= input("word? ")
words = set()
# Using with we don't need to close the file
with open("test.txt") as f:
# here is the difference from your code
for item in f.read().split("\n"):
words.add(item)
def contains(s,words):
for word in words:
if s in word:
print("true")
break
else:
print("false")
contains(s,words)
Typing:
apple returns "true"
ap returns "true"
oeoe returns "false"
The right way is to use a generator for this:
name = input("which file would you like to open? ")
word_to_look_for=input("word? ")
def check(word_to_look_for, word_from_file):
return word_to_look_for == word_from_file
with open(name, "r") as file:
# The code inside the parentheses () returns a generator object
word_exists = (check(word_to_look_for, word_from_file.rstrip("\n")) for word_from_file in file.readlines())
# This will return true if either one of the "check" function results is True
print(any(word_exists))
For first, it's better to open files using
with open(filepath, 'r') as input_file:
you could read about this here
https://docs.python.org/3/tutorial/inputoutput.html
Also you try to add file into set, but you need to add words.
So this is working (and more pythonic) code:
import os
def load_data(filepath):
if not os.path.exists(filepath):
return None
with open(filepath, 'r') as input_file:
text = input_file.read()
return text
if __name__ == '__main__':
filepath = input("Which file would you like to open?")
word=input("What we want to find?")
text = load_data(filepath)
if not text:
print("File is empty or not exists!")
raise SystemExit
words = set(text.split())
print(word in words)
There are a few things going on here which I'm not sure you're totally clear on:
First, F is a file. I'm going to guess that you're intention here is that you're trying to check whether a word is in a file of words (like a dictionary). To do this however you'll need to do something like this:
filename = "somefilename.txt"
words = set()
# open the file
file = open(filename, "r")
# read the lines into an array
lines = file.readlines()
for line in lines:
words.add(line)
testWord = input("word? ")
The second issue is that you're using a function but you're mistaking your parameters as the same variables you've declared in your main flow. Eg.
# this is a verbose way of doing this
words = set()
testWord = input()
def isWordInSet(word, words):
# words here is not the same as the words
# outside the function
if word in words:
print("True")
else:
print("False")
isWordInSet(testWord, words)

Searching or a specific substring in a file

This question is possibly a duplicate but any answers i find don't seem to work. I have a .txt file full of this layout:
artist - song, www.link.com
artist2 - song2, www.link2.com
This is my general purpose:
uinput = input("input here: ")
save = open("save.txt", "w+")
ncount = save.count("\n")
for i in range(0, ncount):
t = save.readline()
if uinput in t:
print("Your string " uinput, " was found in" end = "")
print(t)
My intention is: If the userinput word was found in a line then print the entire line or the link.
You want to read the file, but you are opening the file in write mode. You should use r, not w+
The simplest way to iterate over a file is to have a for loop iterating directly over the file object
Not an error but a nitpick. You do not close your file. You can remedy this with with.. as context manager
uinput = input("input here: ")
with open("save.txt", "r") as f:
for line in f:
if uinput in line:
print('Match found')
You can use list-comprehension to read the file and get only the lines that contain the word, for example:
with open('save.txt', 'r') as f:
uinput = input("input here: ")
found = [line.rstrip() for line in f if uinput.lower() in line.lower()]
if found:
print('Found in these lines: ')
print('\n'.join(found))
else:
print('Not found.')
If you want to print the link only, you can use:
found = [line.rstrip().split(',')[1] for line in f if uinput.lower() in line.lower()]
You can use list comprehension to fetch the lines containing user input words.
use below code:
try:
f = open("file/toyourpath/filename.txt", "r")
data_input = raw_input("Enter your listed song from file :");
print data_input
fetch_line = [line for line in f if data_input in line]
print fetch_line
f.close()
except ValueError, e:
print e

Why my function is returning empty string in python?

What I am doing is, removing all parts of speech except nouns from a text.
I have written a function for that. It may not be the best or optimized code to do that because I have just started coding in python. I am sure the bug must be very basic but I am just not able to figure it out.
In my function two inputs go as parameters. One is the location of text on hard drive and other is the location of file where we want the output.
Following is the code.
def extract_nouns(i_location, o_location):
import nltk
with open(i_location, "r") as myfile:
data = myfile.read().replace('\n', '')
tokens = nltk.word_tokenize(data)
tagged = nltk.pos_tag(tokens)
length = len(tagged)
a = list()
for i in range(0,length):
print(i)
log = (tagged[i][1][0] == 'N')
if log == False:
a.append(tagged[i][0])
fin = open(i_location, 'r')
fout = open(o_location, "w+")
for line in fin:
for word in a:
line = line.replace(word, "")
fout.write(line)
with open(o_location, "r") as myfile_new:
data_out = myfile_new.read().replace('\n', '')
return data_out
When I call this function it is working just fine. I am getting the output on hard disk as I had intended but it does not return the output on the interface or should I say, it is returning a blank string instead of the actual output string.
This is how I am calling it.
t = extract_nouns("input.txt","output.txt")
If you want to try it, take following as the content of input file
"At eight o'clock on
Thursday film morning word line test
best beautiful Ram Aaron design"
This is the output I am getting in the output file (output.txt) when I call the function but the function returns blank string on the interface instead. It does not even print the output.
"
Thursday film morning word line test
Ram Aar design"
You need to close the file first:
for line in fin:
for word in a:
line = line.replace(word, "")
fout.write(line)
fout.close()
Using with is usually the best way to open files as it automatically closes them and file.seek() to go back to the start of the file to read :
def extract_nouns(i_location, o_location):
import nltk
with open(i_location, "r") as myfile:
data = myfile.read().replace('\n', '')
tokens = nltk.word_tokenize(data)
tagged = nltk.pos_tag(tokens)
length = len(tagged)
a = []
for i in range(0,length):
print(i)
log = (tagged[i][1][0] == 'N')
if not log:
a.append(tagged[i][0])
with open(i_location, 'r') as fin, open(o_location, "w+") as fout:
for line in fin:
for word in a:
line = line.replace(word, "")
fout.write(line)
fout.seek(0) # go back to start of file
data_out = fout.read().replace('\n' , '')
return data_out
The last statement in the function should be the return.
Because there is the print data_out, you return the return value of print which is none.
E.g:
In []: def test():
..: print 'Hello!'
..:
In []: res = test()
Hello!
In []: res is None
Out[]: True

How to interact with notepad document correctly in python?

I created a notepad text document called "connections.txt". I need to have some initial information inside it, several lines of just URLs. Each URL has it's own line. I put that in manually. Then in my program I have a function that checks if a URL is in the file:
def checkfile(string):
datafile = file(f)
for line in datafile:
if string in line:
return True
return False
where f is declared at the beginning of the program:
f = "D:\connections.txt"
Then I tried to write to the document like this:
file = open(f, "w")
if checkfile(user) == False:
usernames.append(user)
file.write("\n")
file.write(user)
file.close()
but it hasn't really been working correctly..I'm not sure what's wrong..am I doing it wrong?
I want the information in the notepad document to stay there ACROSS runs of the program. I want it to build up.
Thanks.
EDIT: I found something wrong... It needs to be file = f, not datafile = file(f)
But the problem is... It clears the text document every time I rerun the program.
f = "D:\connections.txt"
usernames = []
def checkfile(string):
file = f
for line in file:
if string in line:
return True
print "True"
return False
print "False"
file = open(f, "w")
user = "aasdf"
if checkfile(user) == False:
usernames.append(user)
file.write("\n")
file.write(user)
file.close()
I was working with the file command incorrectly...here is the code that works.
f = "D:\connections.txt"
usernames = []
def checkfile(string):
datafile = file(f)
for line in datafile:
if string in line:
print "True"
return True
print "False"
return False
user = "asdf"
if checkfile(user) == False:
usernames.append(user)
with open(f, "a") as myfile:
myfile.write("\n")
myfile.write(user)
The code that checks for a specific URL is ok!
If the problem is not erasing everything:
To write to the document without erasing everything you have to use the .seek() method:
file = open("D:\connections.txt", "w")
# The .seek() method sets the cursor to the wanted position
# seek(offset, [whence]) where:
# offset = 2 is relative to the end of file
# read more here: http://docs.python.org/2/library/stdtypes.html?highlight=seek#file.seek
file.seek(2)
file.write("*The URL you want to write*")
Implemented on your code will be something like:
def checkfile(URL):
# your own function as it is...
if checkfile(URL) == False:
file = open("D:\connections.txt", "w")
file.seek(2)
file.write(URL)
file.close()

Categories