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("\"")
Related
How do I remove parentheses and single quotation marks from my output? There is no requirement in my code to show brackets and single quotation marks.
person = " xx "
what_he_said = " abc!"
print(f"{person.split()}曾经说过,“{what_he_said.split()}”")
This is the actual output:
['xx']曾经说过,“['abc!']”
I don't want to output the middle brackets and quotation marks.
I'm using Python 3.10, the IDE is pycharm.
The result of split is a list. If you want the str without surrounding space, you should use strip
person = " xx "
what_he_said = " abc!"
print(f"{person.strip()}曾经说过,“{what_he_said.strip()}”")
Just to print you can try:
Supposing it's necessary to use .split()
person = " xx "
what_he_said = " abc!"
print(*person.split(),'曾经说过',*what_he_said.split())
The split() function will separate the data with the [sep] parameter, it's empty by default, I assume you're using split to remove "[space-k]", but note that it will always return a separate argument list
"hola como estas ".split(sep=" ")
will return:
[hola, como, estas]
If you really need to use the split() function, you can get its values with the index.
E.g
person = " xx "
what_he_said = " abc!"
print(f"{person.split()[0]}曾经说过,“{what_he_said.split()[0]}”")
will return:
xx曾经说过,“abc!”
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
I have a simple string as follows :
str = "id : c40d675f-19a9-4d40-9c6f-223eddafc81d"
Expected output :
c40d675f-19a9-4d40-9c6f-223eddafc81d
What I tried ?
print(str.strip('id : '))
What I get :
c40d675f-19a9-4d40-9c6f-223eddafc81
I am not able to understand, why is the last character 'd' from the end of string is stripped away? When I tried replacing the character 'd' with other alphabets, it works fine.
Because strip() takes a set of characters to remove from both sides of the string. If you have written: str.strip("i18d") it would remove 81d from the end and id from the beginning.
Maybe you wanted to do this :
str.split(":")[1].strip()
strip removes any possible combination of characters provided as its argument from both the start and end of the string. Here, the argument being 'id : ', it strips 'id : ' from the beginning and 'd' (one of the possible combinations of the string 'id : ') from the end of the string.
I think this link should help you.
https://www.tutorialspoint.com/python/string_strip.htm
look at the return value that strip gives
strip() strips the leading or trailing characters of the string off of every character in the sequence.
>>> 'abcadb'.strip('ab')
'cad'
>>> 'www.example.com'.strip('cmowz.')
'example'
strip([chars]):
The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped
Be aware, it removes all combinations of the characters you provided!
>>> str.lstrip("id :")
'c40d675f-19a9-4d40-9c6f-223eddafc81d'
lstrip() returns a copy of the string in which all chars have been stripped from the beginning of the string
Alternatively, You can use str.replace("id : ", "", 1), specify that replace the 1st occurrence of "id : " to empty string
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.