使用 ijson 解析 json 时遇到错误。
背景:我有一系列(大约 1000 个)推特数据的大文件,这些文件以“.bz2”格式压缩。我需要将文件中的元素放入 apd.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 错误,是否可以开发某种错误处理程序来移动到下一个文件 - 如果是这样,任何建议都将不胜感激?
任何帮助将不胜感激。
谢谢你,詹姆斯