如何在python中存储包含单引号(')和双引号(“)的字符串

时间:2011-11-12 19:51:15

标签: python escaping

(+CMGL: 2,"REC READ","DD-Etopup",,"11/11/04,12:48:51+22" Hye! How's it going?) 

我想将此短信息作为字符串存储在python中。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:3)

三重引号 - 这将允许嵌入的单引号和双引号,而不包含转义字符。

s = """(+CMGL: 2,"REC READ","DD-Etopup",,"11/11/04,12:48:51+22" Hye! How's it going?)"""

答案 1 :(得分:1)

反斜杠转义是快速回答:

>>> '(+CMGL: 2,"REC READ","DD-Etopup",,"11/11/04,12:48:51+22" Hye! How\'s it going?)'
'(+CMGL: 2,"REC READ","DD-Etopup",,"11/11/04,12:48:51+22" Hye! How\'s it going?)'
>>> a = '(+CMGL: 2,"REC READ","DD-Etopup",,"11/11/04,12:48:51+22" Hye! How\'s it going?)'
>>> a.split(',')
['(+CMGL: 2', '"REC READ"', '"DD-Etopup"', '', '"11/11/04', '12:48:51+22" Hye! How\'s it going?)']
>>> a.split(',')[5]
'12:48:51+22" Hye! How\'s it going?)'
>>> len(a.split(',')[5])
34