简体   繁体   中英

Python - Remove quotes and double minus using replace

Given a string such as

--"I like salami. "-I want to walk to the shops"

How can I return the string such as

I like salami. I want to walk to the shops.

I can alter each individually but when I use '-"' it fails.

Quotes

b = '"'

for char in b:
    solution = MyString.replace(char,"")

#output --I like salami. -I want to walk to the shops

Minus

b = '-'

for char in b:
    solution = MyString.replace(char,"")

#output "I like salami. "I want to walk to the shops"

Together

MyString = '--"I like salami. "-I want to walk to the shops"'

b = '"-'

for char in b:
    print(MyString.replace(char,""))

#output "I like salami. "I want to walk to the shops"

Just do it like this:

MyString = '--"I like salami. "-I want to walk to the shops"'
MyString = MyString.replace('"',"")
MyString = MyString.replace('-',"")
print MyString
#output: I like salami. I want to walk to the shops

Or you can use Regular expression and do it like this:

import re
MyString = '--"I like salami. "-I want to walk to the shops"'
MyString = re.sub('["-]', '', MyString)
print MyString
#output: I like salami. I want to walk to the shops

Remember to install re if it doesn't exist already. Tell me if there are any problems.

Python string.replace doesn't do an in-place replace, but instead returns a new string which contains the replacement . Since you are not using the return value from the first replace, it is discarded and the second iteration of your loop with the character - replaces just this character and not " , in the original string. For the intended effect, you can use the following snippet:

MyString = '--"I like salami. "-I want to walk to the shops"'
b = '"-'
for char in b:
    MyString = MyString.replace(char,"")
print(MyString)
#Outputs: I like salami. I want to walk to the shops

The .replace(oldstr, newstr) method will help you, and it is simple and daisy-chainable. So...

MyString = MyString.replace('"', '').replace('-', '')

Note that the .replace() method only replaces substrings in one shot. It cannot do individual characters. For that, you could do it with a regular expression, but that's more complicated. That module can be accessed with import re . You could use:

MyString = re.sub('["-]', '', MyString)

Also, on a sidenote... Quoting can be tricky. But there are FOUR different ways you can quote something: in a pair of single quotes, a pair of double quotes, or a pair of triple single-quotes or triple double-quotes. So, all the below are strings in Python:

'hello'
"Don't you think"  # string with an apostrophe is easily in double quotes
'''that this is a really really..'''
"""long quote?  Hey!
    this quote is actually more than one line long!"""
In [1]: MyString = '--"I like salami. "-I want to walk to the shops"'
In [2]: MyString.replace('-', '').replace('"', '')
Out[2]: 'I like salami. I want to walk to the shops'

Here is little different approach using beautiful lambda function:

word='--"I like salami. "-I want to walk to the shops"'
print("".join(list(map(lambda x:x if x!='-' and x!='"' else '',word))))

output:

I like salami. I want to walk to the shops

Another solution using regex:

import re
str = '--"I like salami. "-I want to walk to the shops"'
print re.sub("[-\"]", repl="", string=str)

output:

I like salami. I want to walk to the shops

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM