【发布时间】:2019-06-14 09:31:55
【问题描述】:
我正在使用python3.6。
我需要处理来自文本文件解析的字符串,通常,最多存在字符串中包含双引号的情况。 所以我用replace来处理这个案子。
但我遇到了一个新问题,即文件字段中的单引号空字符串“''”。
例如。
a = '"This is the double quotes in the string"'
# I can handle this simply by
a.replace('"', '')
# But when string is like
b = "''"
b.replace('"', '')
print(b)
>> "''"
#It's ok if I use
b.replace("'", "")
print(b)
>> ""
但我想问一下有没有一种好的/简单的方法可以同时处理 a 和 b 两种情况。
【问题讨论】:
-
使用方法链。例如:
str.replace('"', '').replace("'", "")
标签: python python-3.x string double-quotes single-quotes