gpt4 book ai didi

python - 消除 JSON 文档中键值的前导和尾随反冲

转载 作者:行者123 更新时间:2023-11-30 23:10:23 25 4
gpt4 key购买 nike

我有以下 JSON 结构。

"bent": "{
\"ActiveT\": 6,
\"ErrorM\": \"None\",
\"Except\": \"None\",
\"HadErr\": \"false\",
\"HM\": 62,
\"NHM\": 57,
\"Parameter\": \"14331232706\",
\"ReturnCode\": \"3050\",
\"Severity\": \"info\",
\"Timestamp\": \"Tue July0209: 58: 16NZST2015\",
\"TId\": \"9891319709\",
\"UserInfo\": \"Unknown\",
}"

我想删除 JSON 结构中每个 keyvalue 开头和结尾的 \。我想要使用 python 正则表达式来做到这一点。

最佳答案

使用re.sub

>>> print re.sub(r'\\(?=")', '', string)
"bent": "{
"ActiveT": 6,
"ErrorM": "None",
"Except": "None",
"HadErr": "false",
"HM": 62,
"NHM": 57,
"Parameter": "14331232706",
"ReturnCode": "3050",
"Severity": "info",
"Timestamp": "Tue July0209: 58: 16NZST2015",
"TId": "9891319709",
"UserInfo": "Unknown",
}

正则表达式解释

  • \\ 匹配 \

  • (?=") 正向预测。检查 \ 后面是否跟着 "

    <
  • 将其替换为空字符串。

<小时/>

使用string.replace

>>> print string.replace('\\"', '"')
"bent": "{
"ActiveT": 6,
"ErrorM": "None",
"Except": "None",
"HadErr": "false",
"HM": 62,
"NHM": 57,
"Parameter": "14331232706",
"ReturnCode": "3050",
"Severity": "info",
"Timestamp": "Tue July0209: 58: 16NZST2015",
"TId": "9891319709",
"UserInfo": "Unknown",
}

关于python - 消除 JSON 文档中键值的前导和尾随反冲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30701416/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com