1
(+CMGL: 2,"REC READ","DD-Etopup",,"11/11/04,12:48:51+22" Hye! How's it going?)
我想在python存儲該短信爲一個字符串。我怎樣才能做到這一點?如何存儲在python包含單引號(')和雙引號(「)的字符串
(+CMGL: 2,"REC READ","DD-Etopup",,"11/11/04,12:48:51+22" Hye! How's it going?)
我想在python存儲該短信爲一個字符串。我怎樣才能做到這一點?如何存儲在python包含單引號(')和雙引號(「)的字符串
反斜槓轉義是快速回答:
>>> '(+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
三重引號 - 這將允許嵌入的單和雙引號沒有轉義字符
s = """(+CMGL: 2,"REC READ","DD-Etopup",,"11/11/04,12:48:51+22" Hye! How's it going?)"""
+1 legib。 ility。倒下反斜槓! –
@JohnMachin:這個論點有一個小缺陷,如果你想要字面三倍單/雙引號,你別無選擇,只能逃避它們。 :) –
反斜槓是最後的手段,因爲它們是唯一可以逃脫的東西。我忘了提及三重引號,因爲我只是將它們用於多行字符串。 –