Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JSON value is converted to unicode – python

My current Python Appium test automation framework reads data from JSON file using the json.load()

The value "Ελλάδα" stored in JSON is converted to "Ελλάδα" when json.load() method is called.

Pleases point me to a solution were I can maintain the the actual string value "Ελλάδα"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

JSON data in testdata.json

{
    "country" : {"country_name" : "Ελλάδα"}
}

JSON load in .py file

with open(file_location) as test_data_obj: 
    pytest.data = json.load(test_data_obj)

This prints out "Ελλάδα"

print(pytest.data["country"]["country_name"])

>Solution :

Use with open(file_location,encoding='utf-8'). For whatever reason, your system uses Latin 1 as the default encoding instead of UTF8.

The file is fine. This page, like 99.9% of web pages is UTF8. That’s why you were able to write Ελλάδα without any problem.

Python 3 strings are Unicode too. If you type "Ελλάδα" in a Python console you’ll get the string back.

To get what you posted I had to decode the bytes using latin-1 :

>>> "Ελλάδα".encode('utf-8').decode('latin-1')
'Î\x95λλάδα'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading