【发布时间】:2021-12-04 17:42:54
【问题描述】:
我在用 ijson 解析 json 时遇到了一个错误。
背景:
我有一系列(大约 1000 个)推特数据大文件,这些文件以“.bz2”格式压缩。我需要将文件中的元素放入pd.DataFrame 以进行进一步分析。我已经确定了我需要获取的密钥。我很谨慎地将推特数据放上去。
尝试:
我已设法使用bz2.decompress 使用以下代码解压缩文件:
## Code in loop specific for decompressing and parsing -
with open(file, 'rb') as source:
# Decompress the file
json_r = bz2.decompress(source.read())
json_decom = json_r.decode('utf-8') # decompresses one file at a time rather than a stream
# Parse the JSON with ijson
parser = ijson.parse(json_decom)
for prefix, event, value in parser:
# Print selected items as part of testing
if prefix=="created_at":
print(value)
if prefix=="text":
print(value)
if prefix=="user.id_str":
print(value)
这会产生以下错误:
IncompleteJSONError: parse error: trailing garbage
estamp_ms":"1609466366680"} {"created_at":"Fri Jan 01 01:59
(right here) ------^
两件事:
- 我的解压方法是否正确并为 ijson 解析提供了正确的文件类型(ijson 需要字节和 str)?
- 是 JSON 错误吗? // 如果是 JSON 错误,是否可以开发某种错误处理程序来移动到下一个文件 - 如果可以,任何建议都将不胜感激?
任何帮助将不胜感激。
谢谢你,詹姆斯
【问题讨论】:
标签: python twitter error-handling bz2 ijson