gpt4 book ai didi

python - 如何删除 python 中字符串周围的引号?

转载 作者:太空宇宙 更新时间:2023-11-04 09:44:38 26 4
gpt4 key购买 nike

我正在尝试删除 data["clonedRadarsdetailslist"] 中存在的所有元素的引号,如下所示。

  1. 引用不会被删除

  2. 脚本执行后并非所有元素都存在,可能是因为我正在覆盖

import datetime
data = {
'orgRadar': u'37125110',
'created_on': datetime.datetime(2018, 5, 10, 22, 48, 46, 979953),
'clonedRadarsdetailslist': [
"{'clonedRadar': 40145048, 'clonedStatus': 'PASS', 'clonedRadarFinalStatus': 'PASS', 'updatedFailedReason': 'N/A', 'clonedRadarFinalStatusReason': 'N/A', 'updateStatus': 'PASS', 'clonedStatusfailReason': 'N/A'}",
"{'clonedRadar': 40145054, 'clonedStatus': 'PASS', 'clonedRadarFinalStatus': 'PASS', 'updatedFailedReason': 'N/A', 'clonedRadarFinalStatusReason': 'N/A', 'updateStatus': 'PASS', 'clonedStatusfailReason': 'N/A'}",
],
}
print data
for radardetails in data['clonedRadarsdetailslist']:
radardetails = radardetails.strip('\"')
data['clonedRadarsdetailslist'] = radardetails
print data

当前结果:

{'orgRadar': u'37125110', 'created_on': datetime.datetime(2018, 5, 10, 22, 48, 46, 979953), 'clonedRadarsdetailslist': "{'clonedRadar': 40145054, 'clonedStatus': 'PASS', 'clonedRadarFinalStatus': 'PASS', 'updatedFailedReason': 'N/A', 'clonedRadarFinalStatusReason': 'N/A', 'updateStatus': 'PASS', 'clonedStatusfailReason': 'N/A'}"}

预期结果:

{'orgRadar': u'37125110', 'created_on': datetime.datetime(2018, 5, 10, 22, 48, 46, 979953), 'clonedRadarsdetailslist': [{'clonedRadar': 40145048, 'clonedStatus': 'PASS', 'clonedRadarFinalStatus': 'PASS', 'updatedFailedReason': 'N/A', 'clonedRadarFinalStatusReason': 'N/A', 'updateStatus': 'PASS', 'clonedStatusfailReason': 'N/A'}, {'clonedRadar': 40145054, 'clonedStatus': 'PASS', 'clonedRadarFinalStatus': 'PASS', 'updatedFailedReason': 'N/A', 'clonedRadarFinalStatusReason': 'N/A', 'updateStatus': 'PASS', 'clonedStatusfailReason': 'N/A'}]}

最佳答案

您收到的错误告诉您单引号无效。您可以在尝试加载之前用双引号替换它们:

>>> [json.loads(x.replace("'", '"')) for x in data['clonedRadarsdetailslist']]

[{'clonedRadar': 40145048, 'clonedStatus': 'PASS', 'clonedRadarFinalStatus': 'PASS', 'updatedFailedReason': 'N/A', 'clonedRadarFinalStatusReason': 'N/A', 'updateStatus': 'PASS', 'clonedStatusfailReason': 'N/A'},
{'clonedRadar': 40145054, 'clonedStatus': 'PASS', 'clonedRadarFinalStatus': 'PASS', 'updatedFailedReason': 'N/A', 'clonedRadarFinalStatusReason': 'N/A', 'updateStatus': 'PASS', 'clonedStatusfailReason': 'N/A'}]

此外,改变您正在循环的容器是个坏主意。不要那样做!


编辑:根据要求提供“完整解决方案”。

import datetime
import json

data = {
'orgRadar': u'37125110',
'created_on': datetime.datetime(2018, 5, 10, 22, 48, 46, 979953),
'clonedRadarsdetailslist': [
"{'clonedRadar': 40145048, 'clonedStatus': 'PASS', 'clonedRadarFinalStatus': 'PASS', 'updatedFailedReason': 'N/A', 'clonedRadarFinalStatusReason': 'N/A', 'updateStatus': 'PASS', 'clonedStatusfailReason': 'N/A'}",
"{'clonedRadar': 40145054, 'clonedStatus': 'PASS', 'clonedRadarFinalStatus': 'PASS', 'updatedFailedReason': 'N/A', 'clonedRadarFinalStatusReason': 'N/A', 'updateStatus': 'PASS', 'clonedStatusfailReason': 'N/A'}"
]}

data['clonedRadarsdetailslist'] = [json.loads(x.replace("'", '"'))
for x in data['clonedRadarsdetailslist']]

print(data)

Run online

关于python - 如何删除 python 中字符串周围的引号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50282857/

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