繁体   English   中英

无法在python上正确加载json

[英]can't properly load with json on python

我在.txt文件中有一个具有以下结构的数据库:

['http://legionanime.com/anime/blazblue-alter-memory.html - BlazBlue Alter Memory', 'http://1.bp.blogspot.com/-5d1npGAZFEQ/Ul_DbUa3MNI/AAAAAAAAP70/wAyDB9E7o9U/s1600/images.jpg - BlazBlue Alter Memory', 'http://legionanime.com/anime/blazblue-alter-memory.html - BlazBlue Alter MemoryBlazBlue Alter Memory', 'http://legionanime.com//.htmlA&ntildeo de emision: ']
['http://legionanime.com/anime/gundam-build-fighters.html - Gundam Build Fighters', 'http://2.bp.blogspot.com/-My_c7nCIx5M/Ul24Wo16H6I/AAAAAAAAP7M/zwPbKSVAlC8/s1600/descarga+(1).jpg - Gundam Build Fighters', 'http://legionanime.com/anime/gundam-build-fighters.html - Gundam Build FightersGundam Build Fighters', 'http://legionanime.com//.htmlA&ntildeo de emision: ']

并且此代码应该有效,但无效。

print("starting")
leyendo = open("myfile.txt", "r")
readed = leyendo.read()
leyendo.close()

if not "[" in str(readed):
    print("File got wrong structure")
else:
    print("trying to load lines")
    with open("myfile.txt", 'r') as readinglines:
      for line in readinglines:
        print(line) #this one works
        lineaactual = json.loads(line) #only if this one doesn't exists. Here is the Error
      readinglines.close()
      print("Completed")

我得到的错误是“ ValueError:无法解码JSON对象”,我也不知道为什么。

该数据库是通过使用以下功能修复的html原始代码创建的:

leidofixed = leido.replace('<div class="cont_anime"><div class="anime_box"><a href="', "['")
leidofixed = leidofixed.replace('" title="', " - ")
leidofixed = leidofixed.replace('"><img id="img2" src="', "', '")
leidofixed = leidofixed.replace('" alt="', " - ")
leidofixed = leidofixed.replace('"></a><div></div><span><h1><a href="', "', '")
leidofixed = leidofixed.replace('</a></h1></span><span2><a href="', "', '")
leidofixed = leidofixed.replace('</a></span2></div></div>', "']\n")
leidofixed = leidofixed.replace('">', "")

我做错了什么?

JSON使用双引号,而不是单引号。

尝试:

lineaactual = json.loads(line.replace("'", '"'))

尝试这个:

json.loads(line.strip().replace("'", '"'))

JSON字符串应使用" not '括起来,请参阅JSON规范

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM