json.load() in Python 3 - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: json.load() in Python 3 (/thread-15044.html) |
json.load() in Python 3 - jazmad - Dec-31-2018 Hi I'm trying to adapt some code I've found online for use in my own function. I can get the code to run in Python 2 but would really like to get it to work in Python 3. The code that works in Python 2 is: for ID in id_list: try: with open(location + ID+ '.json','r') as f: data = json.load(f)To run in Python 3, I've tried changing it to (the files are utf-8 encoded): for ID in id_list: try: with open(location + ID+ '.json',encoding = 'utf-8') as f: data = json.load(f)This worked for another occasion where the .json files had ANSI encoding but here I get the error: Any help why this isn't working?Thanks! RE: json.load() in Python 3 - Larz60+ - Dec-31-2018 try (using f-string (if your python 3.6 or newer)): for id in id_list: with open(f'location{id}') as fp: data = json.load(fp)if using python 3 older than 3.6 use: for id in id_list: with open(f'location{}'.format(id)) as fp: data = json.load(fp)FYI: please use 4 space indentation |