2

I wrote a program to convert KML to GeoJSON. But, when I look at the output files, they are written without whitespace, making them very hard to read.

I tried to use the json module like so: file = json.load("<filename>") But it returned the following:

File "/usr/lib/python3.6/json/__init__.py", line 296, in load
    return loads(fp.read())
AttributeError: 'str' has no attribute 'read'
1
  • 1
    Did you read the docs? They tell you what kind of argument that method expects (as does the error message). Commented Jun 21, 2019 at 22:02

1 Answer 1

2

load takes a file object, not a file name.

with open("filename") as fh:
    d = json.load(fh)

Once you've parsed it, you can dump it again, but formatted a bit more nicely

with open("formatted-filename.json", "w") as fh:
    json.dump(d, fh, indent=4)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.