Created
April 8, 2022 15:52
-
-
Save marcelotournier/f5519f5bc0609c9fe1b9a77d0e8d2775 to your computer and use it in GitHub Desktop.
How to read json zip files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import zipfile | |
def read_json_zip(path): | |
""" | |
Reads all json files inside of a zip file | |
and appends the result in a list of dictionaries. | |
""" | |
json_list = [] | |
zfile = zipfile.ZipFile(path) | |
# Clean paths if the zip file was generated by mac os... | |
zpaths = [name for name in zfile.namelist() if '__MACOSX' not in name] | |
for filename in zpaths: | |
file = zfile.open(filename) | |
json_list.append(json.loads(file.read().decode("utf-8"))) | |
file.close() | |
zfile.close() | |
return json_list |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment