-2
votes

The code belonging to this question is available on github.

I've written a script that parses addresses from a csv file, queries the corresponding coordinates (longitude and latitude) using geopy and writes the output to a text file in the JSON format.

The print statement that writes the data into a JSON file is not beautiful:

print('{"type": "Feature","geometry": { "coordinates": ['+str(location.longitude)+
    ','+str(location.latitude)+ ',],"type": "Point"},"properties": {"title": "dentist #1","privat": true,"marker-color": "#6699ff","marker-size": "large","marker-symbol": "dentist"}},')
    time.sleep(0.01)
    file.write('{"type": "Feature","geometry": { "coordinates": ['+str(location.longitude)+
    ','+str(location.latitude)+ ',],"type": "Point"},"properties": {"title": "dentist #1","privat": true,"marker-color": "#6699ff","marker-size": "large","marker-symbol": "dentist"}},')

There must be a better (easier) way to do this. I've started to google around, but am not satisfied with what I'm finding. Does anybody have suggestions on how to handle JSON in Python more gracefully?

2
Yes, as a cursory Google will tell you, there is a perfectly good json module in the standard library. - Daniel Roseman
Why the down vote? So this is not a forum for posting a legitimate question with a code example? I've come accross the json lib, but maybe I just wanted to get a broader picture. - seb
I didn't down vote, but generally people tend to do that when they think the question shows a lack of research effort. - Daniel Roseman
Better to delete the question then. I was asking for a more graceful method, not for the one and only. - seb
May be if you had indicated what google found for you and what specifically you were dissatisfied with, it may have averted the downvotes. - sureshvv

2 Answers

5
votes
1
votes

The json module would serve you well and is recommended. The following will also work:

output = '{{"type": "Feature","geometry": {{ "coordinates": [{},{},],"type": "Point"}},"properties": {{"title": "dentist #1","privat": true,"marker-color": "#6699ff","marker-size": "large","marker-symbol": "dentist"}}}},'.format(location.longitude, location.latitude))

print(output)
time.sleep(0.01)
file.write(output)