Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法读取和加载Python中的JSON文件,JSON格式不同_Python_Json_Python 3.x_Pandas_Dataframe - Fatal编程技术网

无法读取和加载Python中的JSON文件,JSON格式不同

无法读取和加载Python中的JSON文件,JSON格式不同,python,json,python-3.x,pandas,dataframe,Python,Json,Python 3.x,Pandas,Dataframe,我有下面格式的JSON,我无法读取和加载JSON文件。获取额外数据错误。请帮忙 {"From":{"$type":"Microsoft.Bot.Schema.ChannelAccount, Microsoft.Bot.Schema","id":"e203b3e0-0b89- 11ea-b608-f5b0a8eb5137","name":"Bot","aadObjectId":null,"role":"bot"},"To": {"$type":"Microsoft.Bot.Schema.Chan

我有下面格式的JSON,我无法读取和加载JSON文件。获取额外数据错误。请帮忙

{"From":{"$type":"Microsoft.Bot.Schema.ChannelAccount, Microsoft.Bot.Schema","id":"e203b3e0-0b89- 11ea-b608-f5b0a8eb5137","name":"Bot","aadObjectId":null,"role":"bot"},"To": 
{"$type":"Microsoft.Bot.Schema.ChannelAccount, Microsoft.Bot.Schema","id":"deb09575-8edb-411f-b875-418e7944a315","name":"User","aadObjectId":null,"role":"user"},"Text":""}
{"From":{"$type":"Microsoft.Bot.Schema.ChannelAccount, Microsoft.Bot.Schema","id":"e203b3e0-0b89-11ea-b608-f5b0a8eb5137","name":"Bot","aadObjectId":null,"role":"bot"},"To": 
{"$type":"Microsoft.Bot.Schema.ChannelAccount, Microsoft.Bot.Schema","id":"a23f6775-c557-489d-996a-d6d3b329a234","name":"User","aadObjectId":null,"role":"user"},"Text":"Let me know if there’s anything else you need."}
{"From":{"$type":"Microsoft.Bot.Schema.ChannelAccount, Microsoft.Bot.Schema","id":"e203b3e0-0b89-11ea-b608-f5b0a8eb5137","name":"Bot","aadObjectId":null,"role":"bot"},"To": 
{"$type":"Microsoft.Bot.Schema.ChannelAccount, Microsoft.Bot.Schema","id":"a9f3d513-e4d2-48dc-996a-df3c292e707c","name":"User","aadObjectId":null,"role":"user"},"Text":"I’m here if you need anything else."}
{"From":{"$type":"Microsoft.Bot.Schema.ChannelAccount, Microsoft.Bot.Schema","id":"e203b3e0-0b89-11ea-b608-f5b0a8eb5137","name":"Bot","aadObjectId":null,"role":"bot"},"To": 
{"$type":"Microsoft.Bot.Schema.ChannelAccount, Microsoft.Bot.Schema","id":"b08c1237-82cb-4550-8b19-cef5a8e87439","name":"User","aadObjectId":null,"role":"user"},"Text":""}
{"From":{"$type":"Microsoft.Bot.Schema.ChannelAccount, Microsoft.Bot.Schema","id":"e203b3e0-0b89-11ea-b608-f5b0a8eb5137","name":"Bot","aadObjectId":null,"role":"bot"},"To": 
{"$type":"Microsoft.Bot.Schema.ChannelAccount, Microsoft.Bot.Schema","id":"7eb76928-1a0e-4e31-80b2-519be0315d56","name":"User","aadObjectId":null,"role":"user"},"Text":""}
代码:

import json
import pandas as pd
from datetime import datetime
import dateutil.parser

##Date Format
date_format = "%Y-%m-%dT%H:%M:%S:%M" 

#ft = 'C:/Users/abc/Desktop/xyz/pqr/aaa/Data5.json'
#data = json.dumps(ft)
#data = json.load(json.dumps(ft))

##Read the JSON file
with open('C:/Users/abc/Desktop/xyz/pqr/aaa/Data5.json',encoding= "utf-8") as f:
    #data = json.loads(json.dumps([f]))
    data = json.load(f)
错误


如果将json对象放入数组中,并用逗号正确分隔对象:

[
{"From":
    {"$type":"Microsoft.Bot.Schema.ChannelAccount, Microsoft.Bot.Schema",
      "id":"e203b3e0-0b89- 11ea-b608-f5b0a8eb5137",
      "name":"Bot","aadObjectId":null,
      "role":"bot"},
  "To":
    {"$type":"Microsoft.Bot.Schema.ChannelAccount, Microsoft.Bot.Schema",
      "id":"deb09575-8edb-411f-b875-418e7944a315",
      "name":"User",
      "aadObjectId":null,
      "role":"user"},
  "Text":""},
{"From":
    {"$type":"Microsoft.Bot.Schema.ChannelAccount, Microsoft.Bot.Schema",
      "id":"e203b3e0-0b89-11ea-b608-f5b0a8eb5137",
      "name":"Bot",
      "aadObjectId":null,
      "role":"bot"},
  "To":
    {"$type":"Microsoft.Bot.Schema.ChannelAccount, Microsoft.Bot.Schema",
      "id":"a23f6775-c557-489d-996a-d6d3b329a234",
      "name":"User",
      "aadObjectId":null,
      "role":"user"},
  "Text":"Let me know if there’s anything else you need."},
{"From":
    {"$type":"Microsoft.Bot.Schema.ChannelAccount, Microsoft.Bot.Schema",
      "id":"e203b3e0-0b89-11ea-b608-f5b0a8eb5137",
      "name":"Bot",
      "aadObjectId":null,
      "role":"bot"},
  "To":
    {"$type":"Microsoft.Bot.Schema.ChannelAccount, Microsoft.Bot.Schema",
      "id":"a9f3d513-e4d2-48dc-996a-df3c292e707c",
      "name":"User",
      "aadObjectId":null,
      "role":"user"},
  "Text":"I’m here if you need anything else."},
{"From":
    {"$type":"Microsoft.Bot.Schema.ChannelAccount, Microsoft.Bot.Schema",
      "id":"e203b3e0-0b89-11ea-b608-f5b0a8eb5137",
      "name":"Bot",
      "aadObjectId":null,
      "role":"bot"},
  "To":
    {"$type":"Microsoft.Bot.Schema.ChannelAccount, Microsoft.Bot.Schema",
      "id":"b08c1237-82cb-4550-8b19-cef5a8e87439",
      "name":"User",
      "aadObjectId":null,
      "role":"user"},
  "Text":""},
{"From":
    {"$type":"Microsoft.Bot.Schema.ChannelAccount, Microsoft.Bot.Schema",
      "id":"e203b3e0-0b89-11ea-b608-f5b0a8eb5137",
      "name":"Bot",
      "aadObjectId":null,
      "role":"bot"},
  "To":
    {"$type":"Microsoft.Bot.Schema.ChannelAccount, Microsoft.Bot.Schema",
      "id":"7eb76928-1a0e-4e31-80b2-519be0315d56",
      "name":"User",
      "aadObjectId":null,
      "role":"user"},
  "Text":""}
]
这是一个有效的json:

{"From":{"$type":"Microsoft.Bot.Schema.ChannelAccount, Microsoft.Bot.Schema","id":"e203b3e0-0b89- 11ea-b608-f5b0a8eb5137","name":"Bot","aadObjectId":null,"role":"bot"},"To":
   {"$type":"Microsoft.Bot.Schema.ChannelAccount, Microsoft.Bot.Schema","id":"deb09575-8edb-411f-b875-418e7944a315","name":"User","aadObjectId":null,"role":"user"},"Text":""}
但是你有很多这样的方法,所以json在读取一个对象后仍然会找到数据,并且会被阻塞

事实上,您可以一次读取两行文件:

   ...
   ##Read the JSON file
   with open('C:/Users/abc/Desktop/xyz/pqr/aaa/Data5.json',encoding= "utf-8") as f:
       data = []            # initialize an empty list
       for line in f:
           line += next(f)  # concatenate every second line
           data.append(json.loads(line))

看看这是否产生了您需要的数据

>>> new_data = '[' + re.sub(r"\}\s*\{", "}, {", data) + ']'
>>> json_data = json.loads(new_data)
只需要稍微修改一下数据。假设这是几个JSON数据包的连锁,我们可以用适当的语法将它们分开,并将其放入带有方括号的JSON兼容列表中

>>> json_data[2]['From']['$type']
'Microsoft.Bot.Schema.ChannelAccount, Microsoft.Bot.Schema'

您显示的是无效的JSON这些是一堆JSON对象,每一个都是有效的JSON本身。您可能需要在JSON数组中使用这些字符,用逗号
数据[340:360]===>,“Text”:“}\n{“Fro”
…数据的格式不正确。应该有逗号分隔的字典。它仍然会抛出错误,引发JSONDecodeError(“期望值”,s,err.value)from None JSONDecodeError:期望值修复它然后@sanjanajha,您知道如何调试吗?
>>> json_data[2]['From']['$type']
'Microsoft.Bot.Schema.ChannelAccount, Microsoft.Bot.Schema'