额外数据:第1行第2列 [英] Extra Data: Line 1 column 2

查看:148
本文介绍了额外数据:第1行第2列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这已经被问过了,所以这肯定会被标记为重复,但是无论如何.我已经坚持了好几天,其他解决方案都没有做任何事情.我要做的就是将json加载到python程序中.

I know this has been asked before so this is definitely getting marked as duplicate but whatever. I've been stuck on this for days and none of the other solutions have done a thing. All I am trying to do is load the json into a python program.

尝试1:

def get_post(number, target):
#path = str(number) + ".json"

post = json.loads('1.json')

return post

尝试2:

with open("1.json") as infile: post = json.loads("1.json")

两者都不起作用.我知道了

And neither works. I get

  File "posthandler.py", line 34, in <module>
  with open("1.json") as infile: post = json.loads("1.json")
  File "/usr/lib/python3.6/json/__init__.py", line 354, in loads
  return _default_decoder.decode(s)
  File "/usr/lib/python3.6/json/decoder.py", line 342, in decode
  raise JSONDecodeError("Extra data", s, end)

这是我的json:

{
"name" : "test"
}

请注意,我已经查看了文件的十六进制,似乎没有什么错位

Note I have looked at the hex of the file and nothing seems out of place

推荐答案

让我重复我的观点:

这两种尝试几乎都是正确的,但是您应该在load函数调用中使用文件处理程序.

Either attempt is almost right but you should use the file handler in your load function call.

尝试一下:

with open("1.json", "r") as infile: 
    post = json.load(infile)

这篇关于额外数据:第1行第2列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆