如何访问上传的json文件google colab

ekc*_*one 3 python json google-colaboratory

我一直在尝试读取 google colab 中的文件,它应该将文件读取为简单的 JSON,但我什至无法在json.dumps(file)不出现 100 个错误的情况下执行

上传文件:

import json 
import csv 
from google.colab import files
uploaded = files.upload()
Run Code Online (Sandbox Code Playgroud)

打印作品,显示文件内容:

print(uploaded)
data = json.dumps(uploaded)
Run Code Online (Sandbox Code Playgroud)

但当我Object of type 'bytes' is not JSON serializable尝试做时我得到json.dumps(uploaded)

该文件不应该被读取为json和 notbytes吗?在其他一些情况下,我测试它也读作dictionary

JSON 文件:

[
    {
        "type": "message",
        "subtype": "channel_join",
        "ts": "123",
        "user": "DWADAWD",
        "text": "<@DWADAWD> has joined the channel"
    },
    {
        "type": "message",
        "subtype": "channel_join",
        "ts": "123",
        "user": "DWADAWD",
        "text": "<@DWADAWD> has joined the channel"
    },
    {
        "text": "Let's chat",
        "user_profile": {
            "display_name": "XASD",
            "team": "TDF31231",
            "name": "XASD",
            "is_restricted": false,
            "is_ultra_restricted": false
        },
        "blocks": [
            {
                "type": "rich_text",
                "block_id": "2N1",
                "elements": [
                    {
                        "type": "rich_text_section",
                        "elements": [
                            {
                                "type": "text",
                                "text": "Let's chat"
                            }
                        ]
                    }
                ]
            }
        ]
    }
]
Run Code Online (Sandbox Code Playgroud)

Kor*_*ich 5

如果您只上传 1 个文件。您可以从其获取内容values()

data = next(iter(uploaded.values()))
Run Code Online (Sandbox Code Playgroud)

然后,您可以将 json 字符串转换为 dict

d = json.loads(data.decode())
Run Code Online (Sandbox Code Playgroud)

这是一个示例笔记本