在Python中加载json文件时,如何替换不需要的字符[]?

在Python中加载json文件时,如何替换不需要的字符[]?,python,json,Python,Json,我尝试使用以下代码加载json文件: with open('tweets_036.jsonl') as json_file: data = json.load(json_file) 但我得到以下错误消息: JSONDecodeError: Extra data: line 24289 column 2 (char 1088180) 当我查看文件/行24289时,我可以看到[]导致了问题: "favorited": false } ][ {

我尝试使用以下代码加载json文件:

with open('tweets_036.jsonl') as json_file:
    data = json.load(json_file)
但我得到以下错误消息:

JSONDecodeError: Extra data: line 24289 column 2 (char 1088180)
当我查看文件/行24289时,我可以看到[]导致了问题:

        "favorited": false
    }
][
    {
        "retweeted": false,
与前几行相比,应该是:

        "favorited": false
    },
    {
        "retweeted": false,
仅供参考,这些数据与tweet相关,“转发”通常是新tweet的第一个元素,而“收藏”则是最后一个元素


如果您能提供帮助,请提前感谢。

您可以尝试:python的re.sub

它使用以下格式:

result = re.sub(pattern, replacement, input)
因此,在您的情况下,应该是这样的:

clean_data = re.sub('][', '', data)

不久前也有同样的问题。出于某种原因,twitter在一个列表中返回多个json结构,但不使用逗号分隔列表,因此在
[]
之间提取每个json元素,并将它们附加到实际列表中。因此,您将得到一个json结构化数据列表。这是我不久前使用的解决方案:

import json

filename='C:/tweets_043.json'


with open(filename) as json_file:  
    data_str = json_file.read()
    data_str = data_str.split('[',1)[-1]
    data_str = data_str.rsplit(']',1)[0]
    data_str = data_str.split('][')

clean_data = []
need_cleaning = False
for i, each in enumerate(data_str):
    if need_cleaning == True:
        hold_string = hold_string + '][' + each
        if each.strip()[-1] != '}':
            continue
        else:
            clean_data.append(hold_string.strip())
            need_cleaning = False
            continue

    if each.strip()[-1] != '}':
        hold_string = data_str[i]
        need_cleaning = True
        continue

    else:
        clean_data.append(each.strip())
        need_cleaning = False





data = []
error = []
for jsonStr in clean_data:

    jsonStr = '[' + jsonStr + ']'
    try:
        temp_data = json.loads(jsonStr.strip())
        for each in temp_data:
            data.append(each)
    except:
        print ('error')
        error.append(jsonStr)
解析除注释中所述的之外的所有内容。如果问题是:

                        "large": {
                            [{
                                    "is_quote_status": false,
                                    "filter_level": "low",
                                    "in_reply_to_screen_name": "Huawei",
                                    "display_text_range": [
                                        8,
                                        53
                                    ],
我看不到第一个方括号在哪里闭合,或者至少在哪里应该闭合。

使用

tweets_036.jsonl

[
    {
      "favorited": false
    }
][
    {
      "retweeted": false
    }
]
import json

with open('tweets_036.jsonl', 'r') as file:
    content = file.read()
    clean = content.replace('][', ',')  # cleanup here
    json_data = json.loads(clean)

print(json_data)
[{'favorited': False}, {'retweeted': False}]
因此

[
    {
      "favorited": false
    }
][
    {
      "retweeted": false
    }
]
import json

with open('tweets_036.jsonl', 'r') as file:
    content = file.read()
    clean = content.replace('][', ',')  # cleanup here
    json_data = json.loads(clean)

print(json_data)
[{'favorited': False}, {'retweeted': False}]
输出

[
    {
      "favorited": false
    }
][
    {
      "retweeted": false
    }
]
import json

with open('tweets_036.jsonl', 'r') as file:
    content = file.read()
    clean = content.replace('][', ',')  # cleanup here
    json_data = json.loads(clean)

print(json_data)
[{'favorited': False}, {'retweeted': False}]

如果您的数据不是
json
,则不能使用
json.load()
。它是json格式,但其中的特定行有问题。如果它是json格式,则
json.load()
将加载它。也许您的意思是
,而不是
'
Hi Chitown,你帮了我一段时间,并提出了上述建议。问题是,当我使用上面的方法时,在某些情况下会出现错误,因为某些记录中也有这样的内容:“description:”[iphone\u30a2\u30d7\u30ea\u3092\u3055\u304c\u3059]\u306b\u5f53\u305f\u308a\u524d\u306e\u691c\u7d22\u3092\niphone\u30a2\u30d7\u30ea\U30D85!\u691c\u7d22\u30fc\u30fc\u30fc\U655\U751]“u4fa1\U683 3][\\\U3040404040404040404040404040404040404141414140404040404040404040404040404040404040404040404010\U3010 10 10 10 10 10\U3010 10 10 10\U3010 10\U3010\U3010\U3010 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 u3082\u308c\u305f\u30a2\u30d7\u30ea\u3092\u30e9\u30f3\u30c0\u30e0\u306b“,所以我们只想在“单独”的tweet上替换它。嗨,我用DirtyBit的答案再试了一次,似乎效果不错!为了完整起见,我使用了以下代码:with open('tweets036.jsonl','r')as file:content=file.read()clean=content.replace('][',')#cleanup here json_data=json.loads(clean)df=pd.io.json.json\u规范化(json_数据)是的,这里似乎有一些不合适的地方。我能够解析它的其余部分。我将发布我用来解析它的代码,包括一个部分,它将让您查看导致该问题的块。它没有遵循有效的json格式,因此可能只是他们的问题,而不是您的问题。但不完全确定.PS是否对用户有用我只添加了以下行以加载到DF中:DF=pd.io.json.json\u normalize(json\u数据)