通过删除escape ch格式化从URL获取的json数据

2024-06-28 20:29:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我从url获取了json数据,并将其写入文件名urljson.json 我想格式化json数据,删除'\'和result[]键以满足需要 在我的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没有replace属性,我不知道如何删除'result'和最外部的“{}” 请引导我 我想这个文件对象不是用字符串来解析的,我是python的初学者 谢谢你


Tags: 文件数据nonejsonurldataresultsw
3条回答

在将JSON对象写入文件之前整理它。它有很多空白噪声。像这样试试:

urllink = {a.strip():b.strip() for a,b in json.loads(urllink).values()[0][0].items()}
jsonobj = json.loads(json.dumps(urllink))

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

对于所有对象:

jsonlist = []

for dirtyobj in json.loads(urllink)['result']:
     jsonlist.append(json.loads(json.dumps({a.strip():b.strip() for a,b in dirtyobj.items()})))

with open('urljson.json','w') as outfile:
    json.dump(json.loads(json.dumps(jsonlist)), outfile)

不想收拾吗?然后简单地执行以下操作:

jsonobj = json.loads(urllink)

你不能做'\',这是语法错误。第二个'被转义,不被视为右引号。

data.replace('\'," ")

Why can't Python's raw string literals end with a single backslash?

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)

\是json中的转义字符:

enter image description here

可以将json字符串加载到python dict: enter image description here

相关问题 更多 >