Skip to content

Instantly share code, notes, and snippets.

@heyajulia
Last active September 21, 2021 15:28
Show Gist options
  • Save heyajulia/8b6f70274ecbac5946caf5f3d0b1e3a7 to your computer and use it in GitHub Desktop.
Save heyajulia/8b6f70274ecbac5946caf5f3d0b1e3a7 to your computer and use it in GitHub Desktop.
import json
import os.path
import sys
if __name__ == "__main__":
if len(sys.argv) != 2:
sys.exit(f"Usage: {os.path.basename(sys.argv[0])} FILE")
file_name = sys.argv[1]
try:
with open(file_name) as f:
for lineno, line in enumerate(f, start=1):
try:
j = json.loads(line)
except ValueError:
print(
f"Warning: could not parse line {lineno} as JSON",
file=sys.stderr,
)
continue
print(json.dumps(j, indent=4))
except FileNotFoundError:
sys.exit(f"File '{file_name}' not found")
except:
sys.exit(f"An unexpected error occurred: {sys.exc_info()[1]}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment