【发布时间】:2019-12-18 11:38:08
【问题描述】:
我正在尝试从开放 API 获取一些数据:
https://data.brreg.no/enhetsregisteret/api/enheter/lastned
但我很难理解不同类型的对象和转换的顺序。是strings 到bytes,是BytesIO 还是StringIO,是decode('utf-8) 还是@ 987654331@等...?
到目前为止:
url_get = 'https://data.brreg.no/enhetsregisteret/api/enheter/lastned'
with urllib.request.urlopen(url_get) as response:
encoding = response.info().get_param('charset', 'utf8')
compressed_file = io.BytesIO(response.read())
decompressed_file = gzip.GzipFile(fileobj=compressed_file)
现在卡住了,下一行代码该怎么写?
json_str = json.loads(decompressed_file.read().decode('utf-8'))
我的解决方法是,如果我将它写为 json 文件,然后再次读入并转换为 df,那么它就可以工作了:
with io.open('brreg.json', 'wb') as f:
f.write(decompressed_file.read())
with open(f_path, encoding='utf-8') as fin:
d = json.load(fin)
df = json_normalize(d)
with open('brreg_2.csv', 'w', encoding='utf-8', newline='') as fout:
fout.write(df.to_csv())
我发现了很多关于它的帖子,但我仍然很困惑。第一个解释得很好,但我仍然需要用勺子喂食。
Python 3, read/write compressed json objects from/to gzip file
TypeError when trying to convert Python 2.7 code to Python 3.4 code
How can I create a GzipFile instance from the “file-like object” that urllib.urlopen() returns?
【问题讨论】:
-
您的代码存在哪些问题?在我看来,您在 GZipFile 对象上缺少 .decompress()
-
我得到 AttributeError: 'bytes' object has no attribute 'encode'
-
对不起,这是我得到的回溯: AttributeError: 'str' object has no attribute 'read'
标签: python json pandas gzip urllib