【问题标题】:Loading a string file as a json file将字符串文件加载为 json 文件
【发布时间】:2019-02-12 07:37:57
【问题描述】:

我正在尝试将字符串文件加载为 json 格式。

data = '{"name":"xxx", "truncated":false, "text":"hi this is going weird"}'
       '{"name":"yyy", "truncated":false, "text":"I am not able to identify this"}'

上面的数据是从不同的目录加载的,因此看起来像这样。

print(type(data))
out : <class 'str'>
      <class 'str'>

我想使用以下命令将其转换为 json 字典,但它会抛出错误:

data = json.loads((data))

错误:JSONDecodeError: Expecting value: line 2 column 1 (char 1)

【问题讨论】:

  • 这不是有效的 JSON 或 Python。
  • 请发布正确的 MCVE - 您当前的代码 sn-p 无效,因此没有人可以帮助您。
  • 我在导致此问题的每个语句结束后都有一个 '\n'。我已经解决了这个问题。

标签: python json python-3.x


【解决方案1】:

你应该这样打包你的数据:

{
    "data": [{
        "name": "xxx",
        "truncated": false,
        "text": "hi this is going weird"
    }, {
        "name": "yyy",
        "truncated": false,
        "text": "I am not able to identify this"
    }]
}

【讨论】:

  • 你现在可以检查一下吗
  • 数据没有打包,因为每个字符串都取自不同的目录
猜你喜欢
  • 2023-01-20
  • 2011-01-06
  • 1970-01-01
  • 2016-08-28
  • 1970-01-01
  • 2021-08-03
  • 1970-01-01
  • 2015-04-05
相关资源
最近更新 更多