我使用pycurl函数从网站收集数据。按照示例代码,稍作修改后,我会将所有数据放在一个大缓冲区中。它的格式为{" field_id":"数据"," field_id2":" data2",..}我想解析这与文本行类似,类似于csv,每个数据集的一行输出由花括号括起来。如何最好地实现这一点,因为" pythonic"可以吗?哦,我最好在哪里进行UTF-8转换?我怀疑源编码是ISO8859 - 但这是次要问题。
相关代码:
buffer = StringIO()
c = pycurl.Curl()
c.setopt(c.URL, MY_URL)
c.setopt(c.WRITEDATA, buffer)
c.perform()
c.close()
示例数据:
`{"status":"ok","nature":"Liste des champs principaux de tous les terrains publi\u00e9s","count":"1211","liste":
[{"id":"3667","date_modif":"2013-04-03 11:27:00","code_terrain":"LF5155","toponyme":"Champagne Vol Libre","type_terrain":"Base ULM Autorisation OBLIGATOIRE ","latitude":"N 48 56 10","longitude":"E 004 03 42","altitude":"335` ft","pays":"France","region":"Grand-Est","departement":"Marne","ville":"Villeneuve"},
{"id":"5118","date_modif":"2015-08-18 13:17:23","code_terrain":"LF4861","toponyme":"La Canourgue Conques","type_terrain":"A\u00e9rodrome ferm\u00e9.","latitude":"N 44 24 55","longitude":"E 003 17 05","altitude":"2920 ft","pays":"France","region":"Occitanie","departement":"Loz\u00e8re","ville":"La Canourgue"}
答案 0 :(得分:1)
您正在尝试解析JSON文件,可能对查看文档有用https://docs.python.org/2/library/json.html
答案 1 :(得分:0)
import json
result = json.loads('{"something": 1}', encoding="ISO8859") # default encoding is utf-8
print(result["something"])
>>> 1