首页 > 解决方案 > 从文件python将'r'添加到json字符串

问题描述

从文件中读取 json 时遇到以下错误

    json.decoder.JSONDecodeError: Expecting ',' delimiter: line 2 column 1 (char 948)

使用下面的脚本从文件中读取 json 内容

import json
if __name__ == "__main__":

     file_path = "D:\\Freelancing\\Scraping_football_historic_data\\Data\\1.138103502"

     with open(file_path,'r') as datafile:

         dict_data = json.load(datafile)

     print(dict_data) 

在寻找答案时,这个问题有一个答案,建议我在 json 字符串之前添加r 。

在上述情况下如何完成,或者是否有更好的方法来读取文件。?

该文件的内容可以从 pastebin 链接中读取: https ://pastebin.com/ZyyrtcZW

标签: pythonjson

解决方案


您在每个单独的字典之间都缺少逗号,您的数据应该看起来像

....
{"op":"mcm","clk":"5733948534","pt":1514206953689,"mc":[{"id":"1.138103502","rc":[{"ltp":2.02,"id":48756}]}]},
{"op":"mcm","clk":"5739085003","pt":1514309273736,"mc":[{"id":"1.138103502","rc":[{"ltp":2.0,"id":48756}]}]},
{"op":"mcm","clk":"5739711407","pt":1514327265235,"mc":[{"id":"1.138103502","rc":[{"ltp":2.06,"id":48756}]}]},
.....

推荐阅读