0

我在具有以下结构的 .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: No JSON object could be decoded”,我不知道为什么。

该数据库是从使用此函数修复的 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('">', "")

我做错了什么?

4

2 回答 2

4

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

尝试:

lineaactual = json.loads(line.replace("'", '"'))
于 2013-10-21T23:31:22.050 回答
1

试试这个:

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

JSON 字符串应该用"not括起来'。查看JSON 规范

于 2013-10-21T23:30:06.713 回答