How to extract string inside a string in python? - python

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

Related

strip() does not replace all \n

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', '')

Why is strip giving an unexpected result for the given string?

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

How to replace " [ ] ' " with blank space from a string ['flag = no'] with python

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.

Replace the " character with a space

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("\"")

String function to strip the last comma

Input
str = 'test1,test2,test3,'
Ouput
str = 'test1,test2,test3'
Requirement to strip the last occurence of ','
Just use rstrip().
result = your_string.rstrip(',')
str = 'test1,test2,test3,'
str[:-1] # 'test1,test2,test3'
The question is very old but tries to give the better answer
str = 'test1,test2,test3,'
It will check the last character, if the last character is a comma it will remove otherwise will return the original string.
result = str[:-1] if str[-1]==',' else str
Though it is little bit over work for something like that. I think this statement will help you.
str = 'test1,test2,test3,'
result = ','.join([s for s in str.split(',') if s]) # 'test1,test2,test3'
If you have to remove the last comma (also as the last character?) you can do this via the function removesuffix()
Here is an example:
>>>'hello,'.removesuffix(',')
'hello'
Actually we have to consider the worst case also.
The worst case is,
str= 'test1,test2,test3, ,,,, '
for above code, please use following code,
result = ','.join([s.strip() for s in str.split(',') if s.strip()!=''])
It will work/remove the prefix 'comma' also. For example,
str= ' , ,test1,test2,test3, ,,,, '

Categories

Resources