Problem with reading text then put the text to the list and sort them in the proper way - string

Open the file romeo.txt and read it line by line. For each line, split the line into a list of words using the split() method. The program should build a list of words. For each word on each line check to see if the word is already in the list and if not append it to the list. When the program completes, sort and print the resulting words in alphabetical order.
This is the question my problem is I cannot write a proper code and gathering true data, always my code gives me 4 different lists for each raw!
** This is my code**
fname = input("Enter file name: ")
fh = open(fname)
lst = list()
for line in fh:
line=line.rstrip()
line =line.split()
if line in last:
print(true)
else:
lst.append(line)
print(lst)
*** the text is here, please copy and paste in text editor***
But soft what light through yonder window breaks
It is the east and Juliet is the sun
Arise fair sun and kill the envious moon
Who is already sick and pale with grief

You are not checking the presence of individual words in the list, but rather the presence of the entire list of words in that line.
With some modifications, you can achieve what you are trying to do this way:
fname = input("Enter file name: ")
fh = open(fname)
lst = list()
for line in fh:
line = line.rstrip()
words = line.split()
for word in words:
if word not in lst:
lst.append(word)
print(lst)
However, a few things I would like to point out looking at your code:
Why are you using rstrip() instead of strip()?
It is better to use list = [] as opposed to your lst = list(). It is shorter, faster, more Pythonic and avoids the use of this confusing lst variable.
You should want to remove punctuation marks attached to words, eg: ,.: which do not get removed by split()
If you want a loop body to not do anything, use pass. Why are you printing true? Also, in Python, it's True and not true.

Related

Create a list using another list

I have created a list from a file by using .split() method, now I want to loop over this list to get certain strings from it to another empty list. when I use .append() method it copies the entire list with the brackets to the new list which is not what I want. how do I solve this?
this is the code I use:
file_name= input("Enter file name: ")
name= open(file_name)
for word in name:
word=fh.read()
line=word.rstrip()
word=line.split()
for words in word:
lst=list()
if word not in lst:
lst.append(word)
print(lst)
Hope this works.
def fuse(k,n):
k = (5,3,n)
n = []
for i in k:
n.append(i)
return n
print(fuse([5,3],[3,6]))
If you entered ([5,3],[3,6]) as the arguments to fuse, you would get [5,3,[3,6]].
You should pay more attention to your indentation and variable naming.
I think the following is what you tried.
file_name= input("Enter file name: ")
filtered_words = list()
# Open the file with the help of a context manager, which closes the file
# again in the event of an error.
with open(filename) as fd:
# Iterate over all lines of the file.
for line in fd:
# Cut off the white space at the end of the line and split the line
# into tokens, using space as a separator.
words = line.rstrip().split()
# Iterate over all tokens.
for word in words:
if word not in filtered_words:
# Append the token to the new list if it doesn't already exist.
filtered_words.append(word)
print(filtered_words)
If you want to remove all duplicate values, I recommend using a
set().
fname = input("Enter file name: ")
fh = open(fname)
lst=[]
#looping over words in the file
for word in fh:
words=word.split()
#looping through the words in the list
for words in words:
if words not in last:
#append words to the new list
lst.append(words)
lst.sort()
print(lst)

How to remove lines from a file starting with a specific word python3

I am doing this as an assignment. So, I need to read a file and remove lines that start with a specific word.
fajl = input("File name:")
rec = input("Word:")
def delete_lines(fajl, rec):
with open(fajl) as file:
text = file.readlines()
print(text)
for word in text:
words = word.split(' ')
first_word = words[0]
for first in word:
if first[0] == rec:
text = text.pop(rec)
return text
print(text)
return text
delete_lines(fajl, rec)
At the last for loop, I completely lost control of what I am doing. Firstly, I can't use pop. So, once I locate the word, I need to somehow delete lines that start with that word. Additionally, there is also one minor problem with my approach and that is that first_word gets me the first word but the , also if it is present.
Example text from a file(file.txt):
This is some text on one line.
The text is irrelevant.
This would be some specific stuff.
However, it is not.
This is just nonsense.
rec = input("Word:") --- This
Output:
The text is irrelevant.
However, it is not.
You cannot modify an array while you are iterating over it. But you can iterate over a copy to modify the original one
fajl = input("File name:")
rec = input("Word:")
def delete_lines(fajl, rec):
with open(fajl) as file:
text = file.readlines()
print(text)
# let's iterate over a copy to modify
# the original one without restrictions
for word in text[:]:
# compare with lowercase to erase This and this
if word.lower().startswith(rec.lower()):
# Remove the line
text.remove(word)
newtext="".join(text) # join all the text
print(newtext) # to see the results in console
# we should now save the file to see the results there
with open(fajl,"w") as file:
file.write(newtext)
print(delete_lines(fajl, rec))
Tested with your sample text. if you want to erase "this". The startswith method will wipe "this" or "this," alike. This will only delete the text and let any blank lines alone. if you don't want them you can also compare with "\n" and remove them

Displaying and formatting list from external file in Python

I have an external file that I'm reading a list from, and then printing out the list. So far I have a for loop that is able to read through the list and print out each item in the list, in the same format as it is stored in the external file. My list in the file is:
['1', '10']
['Hello', 'World']
My program so far is:
file = open('Original_List.txt', 'r')
file_contents = file.read()
for i in file_contents.split():
print(i)
file.close()
The output I'm trying to get:
1 10
Hello World
And my current output is:
['1',
'10']
['Hello',
'World']
I'm part way there, I've managed to separate the items in the list into separate lines, but I still need to remove the square brackets, quotation marks, and commas. I've tried using a loop to loop through each item in the line, and only display it if it doesn't contain any square brackets, quotation marks, and commas, but when I do that, it separates the list item into individual characters, rather than leave it as one entire item. I also need to be able to display the first item, then tab it over, and print the second item, etc, so that the output looks identical to the external file, except with the square brackets, quotation marks, and commas removed. Any suggestions for how to do this? I'm new to Python, so any help would be greatly appreciated!
Formatting is your friend.
file = open('Original_List.txt', 'r'))
file_contents = file.readlines() # change this to readlines so that it splits on each line already
for list in file_contents:
for item in eval(list): # be careful when using eval but it suits your use case, basically turns the list on each line into an 'actual' list
print("{:<10}".format(i)) # print each item with 10 spaces of padding and left align
print("\r\n") # print a newline after each line that we have interpreted
file.close()

Transform a "multiple line" - function into a "one line" - function

I try to transform a function that consists of multiple lines into a function that only consists of one line.
The multiple-line function looks like this:
text = “Here is a tiny example.”
def add_text_to_list(text):
new_list = []
split_text = text.splitlines() #split words in text and change type from “str” to “list”
for line in split_text:
cleared_line = line.strip() #each line of split_text is getting stripped
if cleared_line:
new_list.append(cleared_line)
return new_list
I 100% understand how this function works and what it does, yet I have trouble implementing this into a valid “oneliner”. I also know that I need to come up with a list comprehension. What I'm trying to do is this (in chronological order):
1. split words of text with text.splitlines()
2. strip lines of text.splitlines with line.strip()
3. return modified text after both of these steps
The best I came up with:
def one_line_version(text):
return [line.strip() for line in text.splitlines()] #step 1 is missing
I appreciate any kind of help.
Edit: Thanks #Tenfrow!
You forgot about if in the list comprehension
def add_text_to_list(text):
return [line.strip() for line in text.splitlines() if line.strip()]

Expected str instance, int found. How do I change an int to str to make this code work?

I'm trying to write code that analyses a sentence that contains multiple words and no punctuation. I need it to identify individual words in the sentence that is entered and store them in a list. My example sentence is 'ask not what your country can do for you ask what you can do for your country. I then need the original position of the word to be written to a text file. This is my current code with parts taken from other questions I've found but I just can't get it to work
myFile = open("cat2numbers.txt", "wt")
list = [] # An empty list
sentence = "" # Sentence is equal to the sentence that will be entered
print("Writing to the file: ", myFile) # Telling the user what file they will be writing to
sentence = input("Please enter a sentence without punctuation ") # Asking the user to enter a sentenc
sentence = sentence.lower() # Turns everything entered into lower case
words = sentence.split() # Splitting the sentence into single words
positions = [words.index(word) + 1 for word in words]
for i in range(1,9):
s = repr(i)
print("The positions are being written to the file")
d = ', '.join(positions)
myFile.write(positions) # write the places to myFile
myFile.write("\n")
myFile.close() # closes myFile
print("The positions are now in the file")
The error I've been getting is TypeError: sequence item 0: expected str instance, int found. Could someone please help me, it would be much appreciated
The error stems from .join due to the fact you're joining ints on strings.
So the simple fix would be using:
d = ", ".join(map(str, positions))
which maps the str function on all the elements of the positions list and turns them to strings before joining.
That won't solve all your problems, though. You have used a for loop for some reason, in which you .close the file after writing. In consequent iterations you'll get an error for attempting to write to a file that has been closed.
There's other things, list = [] is unnecessary and, using the name list should be avoided; the initialization of sentence is unnecessary too, you don't need to initialize like that. Additionally, if you want to ask for 8 sentences (the for loop), put your loop before doing your work.
All in all, try something like this:
with open("cat2numbers.txt", "wt") as f:
print("Writing to the file: ", myFile) # Telling the user what file they will be writing to
for i in range(9):
sentence = input("Please enter a sentence without punctuation ").lower() # Asking the user to enter a sentenc
words = sentence.split() # Splitting the sentence into single words
positions = [words.index(word) + 1 for word in words]
f.write(", ".join(map(str, positions))) # write the places to myFile
myFile.write("\n")
print("The positions are now in the file")
this uses the with statement which handles closing the file for you, behind the scenes.
As I see it, in the for loop, you try to write into file, than close it, and than WRITE TO THE CLOSED FILE again. Couldn't this be the problem?

Resources