gpt4 book ai didi

python - 通过删除转义字符格式化从 URL 获取的 json 数据

转载 作者:行者123 更新时间:2023-12-01 03:21:49 25 4
gpt4 key购买 nike

我已从 url 获取 json 数据并将其写入文件名 urljson.json我想格式化 json 数据,删除 '\' 和结果 [] 键以满足需求在我的 json 文件中,数据是这样排列的

{\"result\":[{\"BldgID\":\"1006AVE \",\"BldgName\":\"100-6th Avenue SW (Oddfellows)          \",\"BldgCity\":\"Calgary             \",\"BldgState\":\"AB \",\"BldgZip\":\"T2G 2C4  \",\"BldgAddress1\":\"100-6th Avenue Southwest                \",\"BldgAddress2\":\"ZZZ None\",\"BldgPhone\":\"4035439600     \",\"BldgLandlord\":\"1006AV\",\"BldgLandlordName\":\"100-6 TH Avenue SW Inc.                                     \",\"BldgManager\":\"AVANDE\",\"BldgManagerName\":\"Alyssa Van de Vorst           \",\"BldgManagerType\":\"Internal\",\"BldgGLA\":\"34242\",\"BldgEntityID\":\"1006AVE \",\"BldgInactive\":\"N\",\"BldgPropType\":\"ZZZ None\",\"BldgPropTypeDesc\":\"ZZZ None\",\"BldgPropSubType\":\"ZZZ None\",\"BldgPropSubTypeDesc\":\"ZZZ None\",\"BldgRetailFlag\":\"N\",\"BldgEntityType\":\"REIT                     \",\"BldgCityName\":\"Calgary             \",\"BldgDistrictName\":\"Downtown            \",\"BldgRegionName\":\"Western Canada                                    \",\"BldgAccountantID\":\"KKAUN     \",\"BldgAccountantName\":\"Kendra Kaun                   \",\"BldgAccountantMgrID\":\"LVALIANT  \",\"BldgAccountantMgrName\":\"Lorretta Valiant                        \",\"BldgFASBStartDate\":\"2012-10-24\",\"BldgFASBStartDateStr\":\"2012-10-24\"}]}

我想要这样的格式

[  
{
"BldgID":"1006AVE",
"BldgName":"100-6th Avenue SW (Oddfellows) ",
"BldgCity":"Calgary ",
"BldgState":"AB ",
"BldgZip":"T2G 2C4 ",
"BldgAddress1":"100-6th Avenue Southwest ",
"BldgAddress2":"ZZZ None",
"BldgPhone":"4035439600 ",
"BldgLandlord":"1006AV",
"BldgLandlordName":"100-6 TH Avenue SW Inc. ",
"BldgManager":"AVANDE",
"BldgManagerName":"Alyssa Van de Vorst ",
"BldgManagerType":"Internal",
"BldgGLA":"34242",
"BldgEntityID":"1006AVE ",
"BldgInactive":"N",
"BldgPropType":"ZZZ None",
"BldgPropTypeDesc":"ZZZ None",
"BldgPropSubType":"ZZZ None",
"BldgPropSubTypeDesc":"ZZZ None",
"BldgRetailFlag":"N",
"BldgEntityType":"REIT ",
"BldgCityName":"Calgary ",
"BldgDistrictName":"Downtown ",
"BldgRegionName":"Western Canada ",
"BldgAccountantID":"KKAUN ",
"BldgAccountantName":"Kendra Kaun ",
"BldgAccountantMgrID":"LVALIANT ",
"BldgAccountantMgrName\":" Lorretta Valiant ",
"BldgFASBStartDate":"2012-10-24",
"BldgFASBStartDateStr":"2012-10-24"
} `
]

我尝试过替换(“\”,“”)但没有任何改变这是我的代码

import json


import urllib2
urllink=urllib2.urlopen("url").read()

print urllink -commented out



with open('urljson.json','w')as outfile:
json.dump(urllink,outfile)


jsonfile='urljson.json'
jsondata=open(jsonfile)

data=json.load(jsondata)
data.replace('\'," ") --commented out
print (data)

但它说 fileobject 没有替换属性,我不知道如何删除“结果”和最外层的“{}”请指导我我认为文件对象没有以某种方式解析为字符串。我是 python 初学者谢谢

最佳答案

JSON 是数据的序列化编码。 urllink=urllib2.urlopen("url").read() 读取该序列化字符串。使用 json.dump(urllink,outfile) 再次序列化该单个序列化 JSON 字符串。您对它进行了双重编码,这就是为什么您会看到那些额外的“\”转义字符。 json 需要转义这些字符,以免将它们与用于分隔字符串的引号混淆。

如果您希望文件保存原始 json,则无需再次编码,只需这样做

with open('urljson.json','w')as outfile:
outfile.write(urllink)

但看起来您只想获取“结果”列表并仅保存它。因此,将 JSON 解码为 python,获取所需的位,然后再次编码。

import json
import codecs
import urllib2

# read a json string from url
urllink=urllib2.urlopen("url").read()

# decode and grab result list
result = json.loads(urllink)['result']

# write the json to a file
with open('urljson.json','w')as outfile:
json.dump(result, outfile)

关于python - 通过删除转义字符格式化从 URL 获取的 json 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41845264/

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