Delete \n charater with Python - python-3.x

I have a list of sentences which have this "\n" character.
[("Types of Third\n-\nParties\n"),("Examples of third\n-\nparties"), ...]
I tried with the following code :
def remove_whitespace(sent_text):
j=0
for i in sent_text:
sent_text[j]=i.rstrip("\n")
j+=1
remove_whitespace(sent_text)
But the \n character didn't disappear.
Any idea please?
Thanks

You can also use list comprehension to remove these unwanted items.
input_list = [("Types of Third\n-\nParties\n"),("Examples of third\n-\nparties")]
def expunge_unwanted_elements(input_variable):
cleaned = [item.replace('\n', ' ').strip() for item in input_variable]
# Do you want to remove the dashes? If so use this one.
# cleaned = [item.replace('\n', '').replace('-', ' ').strip() for item in input_variable]
return cleaned
print (expunge_unwanted_elements(input_list))
# outputs
['Types of Third - Parties', 'Examples of third - parties']
# or this output if you use the other cleaned in the function
['Types of Third Parties', 'Examples of third parties']

Using str.split & str.join
Ex:
data = [("Types of Third\n-\nParties\n"),("Examples of third\n-\nparties")]
for text in data:
text = "".join(text.split("\n"))
print(text)
Output:
Types of Third-Parties
Examples of third-parties

One quick solution is using str.replace.
In your case:
def remove_whitespace(sent_text):
j=0
for i in sent_text:
sent_text[j]=i.replace("\n","")
j+=1

You can use rstrip() function.
If text is coming with \n or \r, text.rstrip() takes these off.

Related

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

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.

How to get rid of extra parentheses, quotes or commas Python

I'm making a discord bot and i'm running in issue with getting list values with out extra parentheses, quotes or commas.
here's the code:
#gets slots for page
def listindexcheck(slot):
if totalitemcount > slot + (page * 9):
return slot + (page * 9)
else:
return 0
if argument == 'show':
#message set up
embed = discord.Embed(
title='title',
description='Here is your inventory',
colour=discord.Colour.red())
for i in range(9):
embed.add_field(name=f"Slot ({listindexcheck(i+1)})", value=f'Dur: {str(item_dur[listindexcheck(i+1)])}\n'
f'Mod: {str(item_mod[listindexcheck(i+1)])}\n'
f'E.lvl: {str(item_level[listindexcheck(i+1)])}\n'
f'*id:{str(item_ids[listindexcheck(i+1)])}*\n')
embed.set_footer(text='page: ' + str(page+1))
msg = await ctx.send(embed=embed)
print (type(str(item_ids[listindexcheck(i+1)])))
and here's output
Type of data before converting it to string is list
i tried to turn values to string type to get rid of at least the quotes but that didn't work
My question is is there a way to just get the values without doing anything extra to it?
Thanks
From the looks of it, item_dur[listindexcheck(i+1), item_mod[listindexcheck(i+1) item_level[listindexcheck(i+1) item_ids[listindexcheck(i+1) seem to be tuples with only one item inside. Why there is a comma inside the parenthesis ? It is because the comma makes the tuple and not the parenthesis.
Therefore, to not get parenthesis and comma when you print them, you can just get the first item of the tuple like this item_dur[listindexcheck(i+1)][0].
After you've turned it into a string you could use the replace() method to remove any extra pieces that you don't want in there. You would replace anything that you don't want in there with '' to simply remove it from the string.
Here's some info on the replace() method: https://www.w3schools.com/python/ref_string_replace.asp
You can use string.replace, and it'll be better if you make a function for this like that:
def delete_brackets(text: str):
return text.replace(')', '').replace(')', '').replace(',', '')
embed.add_field(name=f"Slot ({listindexcheck(i+1)})", value=f'Dur: {delete_brackets(str(item_dur[listindexcheck(i+1)]))}\n'
f'Mod: {delete_brackets(str(item_mod[listindexcheck(i+1)]))}\n'
f'E.lvl: {delete_brackets(str(item_level[listindexcheck(i+1)]))}\n'
f'*id:{delete_brackets(str(item_ids[listindexcheck(i+1)]))}*\n')
So this will work for you.

How do I search for a substring in a string then find the character before the substring in python

I am making a small project in python that lets you make notes then read them by using specific arguments. I attempted to make an if statement to check if the string has a comma in it, and if it does, than my python file should find the comma then find the character right below that comma and turn it into an integer so it can read out the notes the user created in a specific user-defined range.
If that didn't make sense then basically all I am saying is that I want to find out what line/bit of code is causing this to not work and return nothing even though notes.txt has content.
Here is what I have in my python file:
if "," not in no_cs: # no_cs is the string I am searching through
user_out = int(no_cs[6:len(no_cs) - 1])
notes = open("notes.txt", "r") # notes.txt is the file that stores all the notes the user makes
notes_lines = notes.read().split("\n") # this is suppose to split all the notes into a list
try:
print(notes_lines[user_out])
except IndexError:
print("That line does not exist.")
notes.close()
elif "," in no_cs:
user_out_1 = int(no_cs.find(',') - 1)
user_out_2 = int(no_cs.find(',') + 1)
notes = open("notes.txt", "r")
notes_lines = notes.read().split("\n")
print(notes_lines[user_out_1:user_out_2]) # this is SUPPOSE to list all notes in a specific range but doesn't
notes.close()
Now here is the notes.txt file:
note
note1
note2
note3
and lastly here is what I am getting in console when I attempt to run the program and type notes(0,2)
>>> notes(0,2)
jeffv : notes(0,2)
[]
A great way to do this is to use the python .partition() method. It works by splitting a string from the first occurrence and returns a tuple... The tuple consists of three parts 0: Before the separator 1: The separator itself 2: After the separator:
# The whole string we wish to search.. Let's use a
# Monty Python quote since we are using Python :)
whole_string = "We interrupt this program to annoy you and make things\
generally more irritating."
# Here is the first word we wish to split from the entire string
first_split = 'program'
# now we use partition to pick what comes after the first split word
substring_split = whole_string.partition(first_split)[2]
# now we use python to give us the first character after that first split word
first_character = str(substring_split)[0]
# since the above is a space, let's also show the second character so
# that it is less confusing :)
second_character = str(substring_split)[1]
# Output
print("Here is the whole string we wish to split: " + whole_string)
print("Here is the first split word we want to find: " + first_split)
print("Now here is the first word that occurred after our split word: " + substring_split)
print("The first character after the substring split is: " + first_character)
print("The second character after the substring split is: " + second_character)
output
Here is the whole string we wish to split: We interrupt this program to annoy you and make things generally more irritating.
Here is the first split word we want to find: program
Now here is the first word that occurred after our split word: to annoy you and make things generally more irritating.
The first character after the substring split is:
The second character after the substring split is: t

How to loop through a text file and find the matching keywords in Python3

I am working on a project to define a search function in Python3. Goal is to output the keywords from a list and the sentence(s) from adele.txt that contain(s) the keywords.
This is a user defined list, userlist=['looking','for','wanna'],
adele.txt is on the github page, https://github.com/liuyu82910/search
Below is my function. The first loop is to get all the lines in lowercase from adele.txt, second loop to get the each word in lowercase in userlist. My code is not looping correctly. What I want is to loop all the lines in the text and compare with all the words from the list. What did I do wrong?
def search(list):
with open('F:/adele.txt','r') as file:
for line in file:
newline=line.lower()
for word in list:
neword=word.lower()
if neword in newline:
return neword,'->',newline
else:
return False
This is my current result, it stops looping, I only got one result:
Out[122]:
('looking', '->', 'looking for some education\n')
Desired output would be:
'looking', '->', 'looking for some education'
... #there are so many sentences that contain looking
'looking',->'i ain't mr. right but if you're looking for fast love'
...
'for', -> 'looking for some education'
...#there are so many sentences that contain for
'wanna',->'i don't even wanna waste your time'
...
Here:
if neword in newline:
return neword,'->',newline
else:
return False
You are returning (either a tuple or False) on the very first iteration. return means "exit the function here and now".
The simple solution is to store all matches in a list (or dict etc) and return that:
# s/list/targets/
def search(targets):
# let's not do the same thing
# over and over and over again
targets = [word.lower() for word in targets]
results = []
# s/file/source/
with open('F:/adele.txt','r') as source:
for line in source:
line = line.strip().lower()
for word in targets:
if word in line:
results.append((word, line))
# ok done
return results

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()]

Resources