在python中读取文件:json.decoder.JSONDecodeError

时间:2018-12-28 22:08:29

标签: python json

我有一个json文件。

with open('list.json', "r") as f:
    r_list = json.load(f)

崩溃:

  

json.decoder.JSONDecodeError:预期值:第1行第1列(字符   0

我在线检查了架构,该架构有效。

模式非常简单:

{"foo": [
{"name": "AAA\u2019s BBB CCC", "url": "/foome/foo"}
]}

试图玩:

  • 文件编码
  • 尝试虚拟文件

..用尽了想法-“ json.load”期望二进制文件存在吗?


编辑1 代码在普通文件中起作用,在scrapy类中不起作用

import scrapy
from scrapy.selector import Selector
from scrapy.http import HtmlResponse
import json

class myScraper(scrapy.Spider):
    name="testScraper"

    def start_requests(self):        
        with open('test.json') as f:
            self.logger.info(f.read()) #shows the file content
            r_list = json.load(f) # breaks with the error msg
            yield "foo"

    def parse(self, response):
        self.logger.info("foo")

'test.json'

{
    "too": "foo"
}

1 个答案:

答案 0 :(得分:-2)

您的文件很可能为空。

示例:

https://repl.it/@mark_boyle_sp/SphericalImpressiveIrc


已更新:

您的迭代器已用完,如注释中所述。 由于您记录了文件内容,因此迭代器位于文件末尾。 (看起来像一个空文件,因此是例外)

重置迭代器或将内容读取为本地值并对其进行操作。

json_str = f.read()
self.logger.info(json_str) #shows the file content
r_list = json.loads(json_str)

再次更新

(我假设)您在解析方法中遇到的棘手问题是?响应主体是一个字节对象,您需要对其进行解码,并在结果字符串上使用loads,如下所示:

def parse(self, response):
    self.logger.info("foo")
    resp_str = response.body.decode('utf-8') 
    self.logger.info(resp_str) #shows the response
    r_list = json.loads(json_str)
相关问题