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

发布于 2024-12-15 04:42:45 字数 144 浏览 2 评论 0 原文

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

我想将这条短信作为字符串存储在 python 中。我该怎么做?

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

I want to store this sms message in python as a string. How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迎风吟唱 2024-12-22 04:42:45

三引号 - 这将允许嵌入单引号和双引号而无需转义字符。

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

Triple quotes - this will allow embedded single and double-quotes without escape characters.

s = """(+CMGL: 2,"REC READ","DD-Etopup",,"11/11/04,12:48:51+22" Hye! How's it going?)"""
-残月青衣踏尘吟 2024-12-22 04:42:45

反斜杠转义是快速的答案:

>>> '(+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

Backslash escaping is the quick answer:

>>> '(+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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文