[英]Remove string containing apostrophes python
如何删除包含撇号的字符串,例如:我想从文本中删除' Cannot get 'data' from cell'
。 我会使用str.replace('Cannot get 'data' from cell','')
,但是撇号将字符串“拆分”,所以这行不通。
您可以使用反斜杠对单引号进行转义,如下所示:
str.replace('Cannot get \'data\' from cell', '')
如果要同时删除初始引号和中间的引号,则应像这样对第一个和最后一个引号进行转义:
str.replace('\'Cannot get \'data\' from cell\'', '')
只是使用双引号标记要删除的字符串,或使用反斜杠,尽管目前尚不清楚。
string.replace("'Cannot get 'data' from cell'",'')
string.replace('\'Cannot get \'data\' from cell\'', '')
编辑:如果您在Cannot
之前和cell
之后没有引号,则只需从要替换的字符串中删除第一个和最后一个单引号
string.replace("Cannot get 'data' from cell",'')
string.replace('Cannot get \'data\' from cell', '')
我不知道这里的其他答案是否回答了您的问题,但这使我感到困惑。 以什么方式“删除”? 如果您确实要删除它:
foo = 'Hello, World! This string has an \' in it!'
if "'" in foo: # if '\'' in foo is also possible
del foo
如果您打算用某些东西替换撇号,请尝试:
foo = 'Hello, World! This string has an \' in it!'
foo = foo.replace('\'', parameter2) #parameter2 is the value which you wanna replace the apostrophe with
以后请更加具体地回答您的问题!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.