首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用于对字符串中的字符进行转义的反斜杠不计入

如何使用于对字符串中的字符进行转义的反斜杠不计入
EN

Stack Overflow用户
提问于 2020-07-09 20:34:43
回答 1查看 19关注 0票数 1

我有这样一句话:

代码语言:javascript
复制
"My name is Manuel, the "most handsome""

我转义它是因为我想把它放在一个json中,它看起来像这样:

代码语言:javascript
复制
sentence=sentence.replace(""","\"")
"My name is Manuel, the \"most handsome\""

现在我必须采用引号中的位置,也就是:最漂亮的位置,但是给我一个finditer的位置,它计算反斜杠\我不想让它计算它,有没有一种方法我不能计算反斜杠

EN

回答 1

Stack Overflow用户

发布于 2020-07-09 20:38:57

仅将"替换为"sentence = sentence.replace(""",'"')。要获得JSON表示,请使用json模块中的json.dumps()

代码语言:javascript
复制
>>> import json
>>> sentence = "My name is Manuel, the "most handsome""
>>> sentence = sentence.replace(""",'"')
>>> print(sentence)
My name is Manuel, the "most handsome"
>>> sentence.find('"')
23
>>> sentence[23:]
'"most handsome"'
>>> print(json.dumps(sentence))
"My name is Manuel, the \"most handsome\""
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62814931

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档