[英]How to make string when a sentence has quotation marks or inverted commas? Python
For example I have a sentence such as: 例如,我有一个句子,例如:
Jamie's car broke "down" in the middle of the street
How can I convert this into a string without manually removing the quotation marks and inverted commas like: 如何将其转换为字符串,而无需手动删除引号和反逗号,例如:
'Jamies car broke down in the middle of the street'
Any help is appreciated! 任何帮助表示赞赏! Thanks,
谢谢,
Use replace()
one after other: 依次使用
replace()
:
s = """Jamie's car broke "down" in the middle of the street"""
print(s.replace('\'', '').replace('"', ''))
# Jamies car broke down in the middle of the street
You may use regex to remove all special characters from string as: 您可以使用正则表达式从字符串中删除所有特殊字符,如下所示:
>>> import re
>>> my_str = """Jamie's car broke "down" in the middle of the street"""
>>> re.sub('[^A-Za-z0-9\s]+', '', my_str)
'Jamies car broke down in the middle of the street'
Try this: 尝试这个:
oldstr = """Jamie's car broke "down" in the middle of the street""" #Your problem string
newstr = oldstr.replace('\'', '').replace('"', '')) #New string using replace()
print(newstr) #print result
This returns: 返回:
Jamies car broke down in the middle of the street
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.