How do I use Python to convert json to .csv?
-
I have data from Twitter's streaming API. It comes down as JSON, and I need to get it into a .csv for analysis. Python seems to be the way to go. So far, the internet is assuming a level of Python knowledge I do not have. I've found plenty of online tutorials, but none of them make sense. (See: http://mike.teczno.com/notes/streaming-data-from-twitter.html. See: http://stackoverflow.com/questions/5293534/download-json-data-and-convert-it-to-csv-using-python?rq=1 See: http://stackoverflow.com/questions/3976345/convert-json-to-csv?rq=1) My only programming experience is with R, though I am very comfortable with R. So, where do I start? I've gathered I'll need to import the json and csv libraries into Python. Does my script define the fields to pull from the JSON into the .csv? Why I can't I just tell Python to put all the fields into the .csv file? I don't want to become a Python expert; I just want a scaleable script (will have daily Twitter data once my project is running). Thanks! Zack
-
Answer:
What are you trying to do with these tweets, precisely? Take a look at http://docs.python.org/2/library/json.html jsonTweet = json.loads(jsonline) will transform some json into a dict, and each field in the tweet will be a a key or will be nested beneath one of the other keys. createdAt = jsonTweet["created_at"] now you have a createdAt date string Retweet data is harder to extract; you'll need a try/catch to test whether a tweet was retweeted, then retrieve the original tweet data which is in the same format as your jsonedTweet, but nested in a field called "retweet" or something similar. You can get a grip on all the fields using something like print json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4,separators=(',', ': ')) What specific data do you want from the tweet? Writing to csv is easy just make a list of the fields you want in the form
Jacob Jensen at Quora Visit the source
Other answers
Totally untested: import json, csv infile = open("foo.json", "r") outfile = open("bar.csv", "w") writer = csv.writer(outfile) for row in json.loads(infile.read()): writer.write(row) That assumes infile contains data that fits in a CSV file, like a list of lists.
Roberto Alsina
Related Q & A:
- How do I use WordNet in Python?Best solution by Stack Overflow
- How do I create a digital signature and how do I use it?Best solution by support.office.com
- Where do I find my Yahoo briefcase and how do I use it?Best solution by Yahoo! Answers
- How do I make ringtones and convert them to m4r for my iphone?Best solution by Yahoo! Answers
- How do I use chat when I am in a room?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.