exactly, there is just a function definition, you never call it. also, the text argument is not used. better, something within these lines:
import json
def parse(fname):
try:
with open(fname) as f:
return json.load(f)
except ValueError as e:
print('invalid json: %s' % e)
return None
file_name = r"C:\python-script\file\assetLink_tr-TR.json"
print(parse(file_name))
Thanks, that works now. However, if the JSON file is saved as UTF-8 encoding, it will not read the file, the scripts throws up the following message
invalid json: 'charmap' codec can't decode byte 0x81 in position 145717: character maps to <undefined>
I also discovered that if I enter (python -m json.tool "C:\Engineering\test2\assetLink_tr-TR.json") on the CMD line, it works as what I am looking for. But I would still like to find out why the script does not work on UTF-8 JSON files.
Thanks a lot for all the help
Bella
(Feb-27-2020, 08:41 AM)BellaMac Wrote: [ -> ]invalid json: 'charmap' codec can't decode byte 0x81 in position 145717: character maps to <undefined>
I also discovered that if I enter (python -m json.tool "C:\Engineering\test2\assetLink_tr-TR.json") on the CMD line, it works as what I am looking for. But I would still like to find out why the script does not work on UTF-8 JSON files.
Remember on Widows should and most in many cases most specify which encoding that should be used.
If not it will use
charmap
codec.
with open(fname, encoding='utf-8') as f:
The default encoding for Python 3 source code is
utf-8
,but read in and out OS may mess that up.
So as example both read and write do specify what encoding to use,
and that's always
utf-8
if code starts in Python or as a first try in almost all cases.
s = 'Crème and Spicy jalapeño ☂'
with open('unicode.txt', 'w', encoding='utf-8') as f_out:
f_out.write(s)
with open('unicode.txt', encoding='utf-8') as f:
print(f.read())
Output:
Crème and Spicy jalapeño ☂