I'm trying to remove all "\n" in this string. However the string.strip() method does not entirely clean the text
body = "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nSome text\n\nHow toremovealln?\n\t\t\t\t\tbecause notworking\n\t\t\t\t\t"
body.strip("\n")
The result is
"Some text\n\nHow toremovealln?\n\t\t\t\t\tbecause notworking\n\t\t\t\t\t"
How to remove them all ?
You have '\n' and '\t' to be replaced by '' and ' ' respectively. So you can use
body1 = body.replace("\n",'')
body2 = body1.replace("\t",' ')
Use string.replace to replace '\n' for '' (empty string):
body = "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nSome text\n\nHow toremovealln?\n\t\t\t\t\tbecause notworking\n\t\t\t\t\t"
print(body.replace('\n', ''))
If you want to remove only the duplicate line feeds, you can use a regex through re.sub:
re.sub(r'([\n])\1+', '', body))
Or to remove them all:
re.sub(r'\n', '', body)
Use string.replace() not strip:
This method will replace the old char with a new char. In your case, you want to 'replace' the new line '\n' with nothing ''. As seen below
body.replace('\n', '')
this will return a new string which you can re-assign to body:
body = body.replace('\n', '')
now body is:
'Some textHow toremovealln?\t\t\t\t\tbecause notworking\t\t\t\t\t'
so if you finally want to remove the tabs '\t' you can just do a further string.replace() on them as you said above:
body = body.replace('\t', '')
Related
my code:
readfile = open("{}".format(file), "r")
lines = readfile.read().lower().split()
elements = """,.:;|!##$%^&*"\()`_+=[]{}<>?/~"""
for char in elements:
lines = lines.replace(char, '')
this works and removes the special characters. but I need help with striping "-" and " ' "
so for example " saftey-dance " would be okay but not " -hi- " but " i'll " is okay but not " 'hi "
i need to strip only the beginning and ending
its not a string it is a list.
how do I do this?
May be you can try string.punctuation and strip:
import string
my_string_list = ["-hello-", "safety-dance", "'hi", "I'll", "-hello"]
result = [item.strip(string.punctuation) for item in my_string_list]
print(result)
Result:
['hello', 'safety-dance', 'hi', "I'll", 'hello']
First, using str.replace in a loop is inefficient. Since strings are immutable, you would be creating a need string on each of your iterations. You can use str.translate to remove the unwanted characters in a single pass.
As to removing a dash only if it is not a boundary character, this is exactly what str.strip does.
It also seems the characters you want to remove correspond to string.punctuation, with a special case for '-'.
from string import punctuation
def remove_special_character(s):
transltation = str.maketrans('', '', punctuation.replace('-', ''))
return ' '.join([w.strip('-') for w in s.split()]).translate(transltation)
polluted_string = '-This $string contain%s ill-desired characters!'
clean_string = remove_special_character(polluted_string)
print(clean_string)
# prints: 'This string contains ill-desired characters'
If you want to apply this to multiple lines, you can do it with a list-comprehension.
lines = [remove_special_character(line) for line in lines]
Finally, to read a file you should be using a with statement.
with open(file, "r") as f
lines = [remove_special_character(line) for line in f]
I am using Sublime Text version 3.0 and my input is:
little_known_person = "' Christopher '"
print(little_known_person.strip())
but my output is ' Christopher ' instead of 'Christopher'. The output is the same if I try the rstrip() and lstrip() methods.
You need to strip leading/trailing whitespace and single quotes. The default, with no argument, is to strip only whitespace.
little_known_person.strip(" '")
The argument is an arbitrary iterable (str, list, tuple, etc) containing characters that should be stripped. The order doesn't matter.
You need to strip whitepaces and single quotes and then re-add the quotes:
little_known_person = "'" + little_known_person.strip(" '") + "'"
Please try if the following approach solves your issue:
>>> little_known_person = "' Christopher '"
>>> little_known_person.replace("'", "").strip()
'Christopher'
Need a solution for bellow code.
variable = ' "value" '
How to get variable = 'value'
Thanks
try:
variable.replace("\"","").strip()
replace replaces the double quotes with nothing (removes it) and strip() removes the trailing and leading spaces
You can try this:
s = ' "value" '
s = s[2:-2]
print(s)
Output:
value
Trying to replace the bracket and single quotes with space using re.sub, its throwing error from ['flag = no']
import re
import subprocess
#string to search text
lst = r'(flask) C:\Users\user1\AppData\Local\Programs\Python\Python35-enter code heretion>python secureassistchk.py flag = no'
#search flag = no within string & return "['flag = no']
dat = re.findall('flag.*', lst)
print("Print FLAG:", dat)
# replace [' with blank space , this doesn't work
#dat3 = re.sub('[\(\)\{\}<>]', '', dat)
#dat3 = re.sub('\b[]\b','', dat)
dat3 = re.sub('[ ]','',dat)
print("Print FLAG:", dat3)
The error is caused by the fact that dat is a list, not a string.
Try:
dat = re.findall('flag.*', lst)[0]
Here, I fixed it for you:
Code:
dat3 = re.sub('\[|\]','', str(dat))
print("Print FLAG:", dat3)
Result:
"'flag = no'"
Edit:
Ok, I missed the part about quotes. This is the corrected regex:
dat3 = re.sub('\[|\]|\'','', str(dat))
The first problem in your initial query was explained by Maciek:
dat is not a string object.
The second problem with your query was that the character you want to replace must be escaped with a \ if they are special characters. You must also chain them with a pipe (a.k.a '|' character).
For example, if you want to add white spaces to your list of replaced characters, the regex will be changed to:
dat3 = re.sub('\[|\]|\'| ','', str(dat))
You should notice the additional pipe and space character.
I have a CSV file, where each comma delimited field is enclosed in " - eg. "fred", "bert", "blah". I am trying to use the replace function but can't seem to have it recognize the " character. example, if the record is in a string called buffer:
buffer.replace('\"','')
Add space between double quotes
p = '"fred", "bert", "blah"'
p.replace('\"'," ")
' fred , bert , blah '
Why do you escape the double quotes if it's inside single quotes ?
Try the following :
a = '"my string"'
a = a.replace('"',' ')
print(a)
#=> ' my string '
You are not replacing it with space firstly, but with empty string
Try using buffer.strip("\"")