0

i got a database in a .txt file with this structure:

['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: ']

and this code should work but doesn't.

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")

The Error i am getting is "ValueError: No JSON object could be decoded" and i got no idea why.

The database was created from a html raw code fixed with this function:

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('">', "")

What i'm doing wrong?

2
  • use simplejson if you can. It produces better error messages Commented Oct 21, 2013 at 23:32
  • 1
    Who told you that file was JSON? Because it's not. If the code that generates it is your code, and it's supposed to be generating JSON, fix it. If it's generating something different that's just sort of similar to JSON parse it by using a parser appropriate to the format (e.g., if it's Python literals displayed by repr, parse it with ast.literal_eval), rather than pretending it's a different format than it is. Commented Oct 21, 2013 at 23:55

2 Answers 2

4

JSON uses double quotes, not single quotes.

Try:

lineaactual = json.loads(line.replace("'", '"'))
Sign up to request clarification or add additional context in comments.

1 Comment

EPIC FACEPALM. that was a big fail on my side, didn't noticed. Tried everything except that lol. Thanks for the help.
1

Try this:

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

JSON string should be enclosed with " not '. See JSON specs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.