Task:
Create a solution that accepts an input identifying the name of a text file, for example, "WordTextFile1.txt". Each text file contains three rows with one word per row. Using the open() function and write() and read() methods, interact with the input text file to write a new sentence string composed of the three existing words to the end of the file contents on a new line. Output the new file contents.
The solution output should be in the format
cat
chases
dog
cat chases dog
the "WordTextFile1.txt" has only 3 words each in a different row
cat
chases
dog
This is what I have which works however the last line with the sentence has an extra whitespace which is breaking my program. What can I do to get rid of the whitespace and fix my code? help!
file = input()
with open(file, "r+") as f:
list_words = f.readlines()
for word in list_words:
print(word.strip())
for word in list_words:
print(word.strip(), end = " ")
this is current output:
student
reads
book
student reads book(extra whitespace)
You are properly removing the last white space by word.strip() but adding end = " " just adds the last whitespace again. Change it to:
file = input()
with open(file, "r+") as f:
list_words = f.readlines()
# I don't see any reason having this for loop
# for word in list_words:
# print(word.strip())
print(' '.join(word.strip() for word in list_words) # this should work
Edit: Removed the list as it was not required. Thanks to #PranavHosangadi
Related
I am learning Python on an app called SoloLearn, got to solve this exercise and I cannot see the solution or see the comments, I don't need to solve it to continue but I'd like to know how to do it.
Book Titles: You have been asked to make a special book categorization program, which assigns each book a special code based on its title.
The code is equal to the first letter of the book, followed by the number of characters in the title.
For example, for the book "Harry Potter", the code would be: H12, as it contains 12 characters (including the space).
You are provided a books.txt file, which includes the book titles, each one written on a separate line.
Read the title one by one and output the code for each book on a separate line.
For example, if the books.txt file contains:
Some book
Another book
Your program should output:
S9
A12
Recall the readlines() method, which returns a list containing the lines of the file.
Also, remember that all lines, except the last one, contain a \n at the end, which should not be included in the character count.
I tried:
file = open("books.txt","r")
for line in file:
for i in range(len(file.readlines())):
title = line[0]+str(len(line)-1)
print(titulo)
title = line[0]+str(len(line)-1)
print(title)
file.close
I also tried with range() and readlines() but I don't know how to solve it
This uses readlines():
with open('books.txt') as f: # Open file
for line in f.readlines(): # Iterate through lines
if line[-1] == '\n': # Check if there is '\n' at end of line
line = line[:-1] # If there is, ignore it
print(line[0], len(line), sep='') # Output first character and length
But I think splitlines() is easier, as it doesn't have the trailing '\n':
with open('books.txt') as f: # Open file
for line in f.read().splitlines(): # Iterate through lines
# No need to check for trailing '\n'
print(line[0], len(line), sep='') # Output first character and length
You can use "with" to handle file oppening and closing.
Use rstrip to get rid of '\n'.
with open('books.txt') as f:
lines = file.readlines()
for line in lines:
print(line[0] + str(len(line.rstrip())))
This is the same:
file = open('books.txt')
lines = file.readlines()
for line in lines:
print(line[0] + str(len(line.rstrip())))
file.close()
This is for a project I am working on and I have simplified my issue. I have two files at hand. First file contains a list of terms such as:
dog
apple
gold
boy
tomato
My second file contains a paragraph possibly containing the terms found in 1st file, but does not have to. Example:
the dog for some reason had a grand
appetite for eating golden apples,
but the dog did not like eating tomatoes
the dog only likes eating gold colored foods
My goal is to open the 1st file, assign variable "wanted_word" the term on the first line (in this case "dog"). Then I want to search for this "wanted_word" in the second file in each line. If the string is found, I want to create a file that contains the first 3 terms of the line that the "wanted_word" is found in. So the output I want would be:
the dog for
but the dog
the dog only
With my current code, I can achieve this. My issue is that after the file is created, I want to move onto the string on the next line in the first file (in this case: "apple"). The idea of the code is to repeat the whole process, create a new file each time a string in first file is found in the second file. If the string is not in the 2nd file, then I want the program to move onto next line.
My code so far:
def word_match(Listofwords, string):
wordnumber = 0
listOfAssociatedWords = []
with open(Listofwords, 'r') as read_obj:
for line in read_obj:
wordnumber += 1
if string in line:
listOfAssociatedWords.append(line.split()[:3])
return listOfAssociatedWords
#------------------------------------------------------------------------------
Firstfile = open("/Directory/firstfilename", "r")
wanted_word = Firstfile.readline(3) #This part also undermines my goal since I limit the read to 3 chars
Firstfile.close()
#------------------------------------------------------------------------------
matched_words = word_match("/Directory/secondfilename", wanted_word)
NewFile = open(wanted_word + '.txt', "w") #this is for the file creation part
for elem in matched_words:
NewFile.write(elem[0] + " " + elem[1] + " " + elem[2])
NewFile.write("\n")
So at the end, by this logic I would have 4 files, with the exception of "boy" which was not in the second file. I am aware I need a loop, but my inexperience with Python requires a need for help.
You need to loop over words, and inside of the loop go over each line:
with open("/Directory/firstfilename") as words:
for word in words:
found_lines = []
with open("/Directory/secondfilename") as lines:
for line in lines:
if word in line:
found_lines.append(' '.join(line.split()[:3]))
if found_lines:
with open(word + '.txt', 'w') as out_file:
for line in found_lines:
out_file.write(line + '\n')
This should write a new file "wordsX" for every mathing word in your paragraph list
def wordMatch(wordListFileLocation, paragraphListFileLocation):
fileCounter = 0
with open(paragraphListFileLocation) as file:
paragraphs = file.readlines()
with open(wordListFileLocation) as wordListFile:
for word in wordListFile:
matching = [s for s in paragraphs if word in s]
if len(matching):
with open('words{0}.txt'.format(fileCounter), 'w') as newFile:
words = matching[0]
firstWords = words[0:3]
line = firstWords.join(' ')
newFile.write(line)
fileCounter += 1
I have a really long list of words that are on each line. How do I make a program that takes in all that and print them all side by side?
I tried making the word an element of a list, but I don't know how to proceed.
Here's the code I've tried so far:
def convert(lst):
return([i for item in lst for i in item.split()])
lst = [''' -The list of words come here- ''']
print(convert(lst))
If you already have the words in a list, you can use the join() function to concatenate them. See https://docs.python.org/3/library/stdtypes.html#str.join
words = open('your_file.txt').readlines()
separator = ' '
print(separator.join(words))
Another, a little bit more cumbersome method would be to print the words using the builtin print() function but suppress the newline that print() normally adds automatically to the end of your argument.
words = open('your_file.txt').readlines()
for word in words:
print(word, end=' ')
Try this, and example.txt just has a list of words going down line by line.
with open("example.txt", "r") as a_file:
sentence = ""
for line in a_file:
stripped_line = line.strip()
sentence = sentence + f"{stripped_line} "
print(sentence)
If your input file is really large and you cant fit it all in memory, you can read the words lazy and write them to disk instead of holding the whole output in memory.
# create a generator that yields each individual line
lines = (l for l in open('words'))
with open("output", "w+") as writer:
# read the file line by line to avoid memory issues
while True:
try:
line = next(lines)
# add to the paragraph in the out file
writer.write(line.replace('\n', ' '))
except StopIteration:
break
You can check the working example here: https://replit.com/#bluebrown/readwritewords#main.py
I want print my output to text file. But the results different if I print in terminal. My code :
...
words = keywords.split("makan","Rina")
sentences = text.split(".")
for itemIndex in range(len(sentences)):
for word in words:
if word in sentences[itemIndex]:
print('"' + sentences[itemIndex] + '."')
break
The ouput like this :
"Semalam saya makan nasi padang."
" Saya makan bersama Rina."
" Rina pesan ayam goreng."
If I add print to text file :
words = ["makan","Rina"]
sentences = text.split(".")
for itemIndex in range(len(sentences)):
for word in words:
if word in sentences[itemIndex]:
with open("corpus.txt",'w+') as f:
f.write(sentences[itemIndex])
f.close()
The output just :
Rina pesan ayam goreng
Why? How to print outputs to text file same like I print outputs in terminal?
You are reopening the file for each iteration of the loop so when you write to it you overwrite what is already there. You need to open the file outside of all the loops and open it in append mode, denoted by a.
When you finish you will end up with only the last line in the file. Remember to close the file using f.close() when you are done with it.
You have to reorder the lines of your code, by moving opening/closing the file outside of the loop:
with open("corpus.txt",'w+') as f:
words = ["makan","Rina"]
sentences = text.split(".")
for itemIndex in range(len(sentences)):
for word in words:
if word in sentences[itemIndex]:
f.write(sentences[itemIndex])
Also, print usually added a newline character after the output, if you want your sentences to be written on the different lines in the file, you may want to add f.write('\n') after every sentence.
Because you are listing with open inside of the loop, and you're using 'w+' mode, your program is going to overwrite the file each time, so you will only end up with the last line written to the file. Try it with 'a' instead, or move with open outside of the loop.
You don't need to call close on a file handle that you have opened using the with syntax. The closing of the file is handled for you.
I would open the file just once before for loops (the for loops should be within the with statement) instead of opening it multiple times. You are overwriting the file each time you are opening it to write a new line.
Your code should be:
words = ["makan","Rina"]
sentences = text.split(".")
with open("corpus.txt",'w+') as f:
for itemIndex in range(len(sentences)):
for word in words:
if word in sentences[itemIndex]:
f.write(sentences[itemIndex] + '\n')
In my code, I want to insert words into a text file from a user. So I have these words in the text file that must be replaced by the user input, here are the strings must be replaced in the file , adjective,plural_noun,noun.
file1 = open('Sample.txt', 'w')
*adjective*,*plural_noun*,*noun*,*verb*,*male_first_name* = [
line.strip() for line in open('Sample.txt')]
for t in *adjective* :
print(input("enter an adjective: ", file=file1))
print(input("enter an plural noun: ", file=file1))
print(input("enter an verb: ", file=file1))
file1.close()
A little something to get you started...
file1 = open('Sample.txt', 'r')
text = file1.read()
while (text.find('*replace*') != -1):
inp = raw_input("enter some text to replace: ");
text = text.replace('*replace*', inp, 1)
print(text)
If Sample.txt contains This is some text to *replace* and the user input is xyz, this code prints:
This is some text to xyz
Let's step through it bit by bit:
file1 = open('Sample.txt', 'r') opens the file for reading ('r' means "for reading").
text = file1.read() reads the content of the file and puts it in the variable text.
while (text.find('*replace*') != -1): looks for occurrences of the string *replace* and continues with the indented commands as long as it finds one.
inp = raw_input("enter some text to replace: "), which only runs if there is a remaining occurrence of *replace*, gets user input and puts it in the variable inp.
text = text.replace('*replace*', inp, 1), which also only runs if there is a remaining occurrence of *replace*, replaces the next occurrence of *replace* with the user input, overwriting the old text.
print(text), which runs once all occurrences of *replace* have been replaced with user input, prints out the new text.
This is not how you would write an efficient programme with lots of different *string* strings, but hopefully it will lead you in the right direction and walking before running is often a good idea.
There is excellent online Python documentation and you can also use the pydoc tool -- e.g. pydoc str.replace from the command line.