[英]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. 如果尚不存在,请记住安装re 。 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 . Python string.replace
不执行就地替换,而是返回一个包含替换的新字符串。 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. .replace(oldstr, newstr)
方法将为您提供帮助,它很简单并且可以通过菊花链链接。 So... 所以...
MyString = MyString.replace('"', '').replace('-', '')
Note that the .replace()
method only replaces substrings in one shot. 请注意, .replace()
方法仅一次替换子字符串。 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
. 可以使用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: 因此,以下所有都是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: 这是使用美丽的lambda函数的几种方法:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.