Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

load .json into python; UnicodeDecodeError

Tags:

python

json

I am trying to load a json file into python with no success. I have been googling a solution for the past few hours and just cannot seem to get it to load. I have tried to load it using the same json.load('filename') function that has worked for everyone. I keep getting : "UnicodeDecodeError: 'utf8' codec can't decode byte 0xc2 in postion 124: invalid continuation byte"

Here is the code I am using

import json
json_data = open('myfile.json')
for line in json_data:
    data = json.loads(line) <--I get an error at this. 

Here is a sample line from my file

{"topic":"security","question":"Putting the Biba-LaPadula Mandatory Access Control Methods to Practise?","excerpt":"Text books on database systems always refer to the two Mandatory Access Control models; Biba for the Integrity objective and Bell-LaPadula for the Secrecy or Confidentiality objective.\n\nText books ...\r\n        "}

What is my error if this seems to have worked for everyone in every example I have googled?

like image 635
user3890141 Avatar asked Dec 07 '14 07:12

user3890141


People also ask

How do I import a json object into Python?

It's pretty easy to load a JSON object in Python. Python has a built-in package called json, which can be used to work with JSON data. It's done by using the JSON module, which provides us with a lot of methods which among loads() and load() methods are gonna help us to read the JSON file.

How do I load a json string in Python?

Use the json.loads() function. The json. loads() function accepts as input a valid string and converts it to a Python dictionary. This process is called deserialization – the act of converting a string to an object.


1 Answers

Have you tried:

json.loads(line.decode("utf-8"))

Similar question asked here: UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2

Edit: If the above does not work,

json.loads(line.decode("utf-8","ignore"))

will.

like image 171
Academiphile Avatar answered Oct 01 '22 16:10

Academiphile