简体   繁体   English

当句子带有引号或逗号反引号时如何制作字符串? 蟒蛇

[英]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.

相关问题 如何替换python中包含引号的字符串? - How to replace a string contains quotation marks in python? 有没有办法忽略 python 中引号内的逗号? - Is there a way to ignore commas inside of quotation marks in python? 在 python 中,如何将 append 一个不带引号的字符串放在一个带单引号的字符串旁边? - In python, how to append a string without quotation marks next to a string with the single quotation marks? 在python字符串中转义引号 - Escaping quotation marks in python string 将字符串写入文件 python 时在文件内保留引号 - Keep quotation marks inside the file when writing string into a file python 如何给字符串加上引号 - how to put quotation marks for string 如何提取由&lt;&gt;界定并以逗号分隔的引号引起的元素列表-python,regex? - How do i extract a list of elements encased in quotation marks bounded by <> and delimited by commas - python, regex? 当两个列表组合在一起时如何从列表项中删除括号、逗号和引号 - How to remove parenthesis, commas, and quotation marks from list items when two lists are combined together 如何在python列表中的字符串周围替换双引号? - How to replace the double quotation marks around a string in python list? 如何将不带引号的字符串转换为python数组? - How to convert string without quotation marks to the python array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM