Skip to content

Instantly share code, notes, and snippets.

@rakashi
Last active November 18, 2017 09:00
Show Gist options
  • Save rakashi/527f18e38e000c3c5ece93d2d3f72582 to your computer and use it in GitHub Desktop.
Save rakashi/527f18e38e000c3c5ece93d2d3f72582 to your computer and use it in GitHub Desktop.
converting json format dataset into csv format
#datafromat looks like below format
# Automotive_5.json
# {"reviewerID": "A3F73SC1LY51OO", "asin": "B00002243X", "reviewerName": "Alan Montgomery", "helpful": [4, 4], "reviewText": "I needed a set of jumper cables for my new car and these had good reviews and were at a good price. They have been used a few times already and do what they are supposed to - no complaints there.What I will say is that 12 feet really isn't an ideal length. Sure, if you pull up front bumper to front bumper they are plenty long, but a lot of times you will be beside another car or can't get really close. Because of this, I would recommend something a little longer than 12'.Great brand - get 16' version though.", "overall": 5.0, "summary": "Work Well - Should Have Bought Longer Ones", "unixReviewTime": 1313539200, "reviewTime": "08 17, 2011"}
# {"reviewerID": "A3F73SC1LY51OO", "asin": "B00002243X", "reviewerName": "Alan Montgomery", "helpful": [4, 4], "reviewText": "I needed a set of jumper cables for my new car and these had good reviews and were at a good price. They have been used a few times already and do what they are supposed to - no complaints there.What I will say is that 12 feet really isn't an ideal length. Sure, if you pull up front bumper to front bumper they are plenty long, but a lot of times you will be beside another car or can't get really close. Because of this, I would recommend something a little longer than 12'.Great brand - get 16' version though.", "overall": 5.0, "summary": "Work Well - Should Have Bought Longer Ones", "unixReviewTime": 1313539200, "reviewTime": "08 17, 2011"}
# {"reviewerID": "A3F73SC1LY51OO", "asin": "B00002243X", "reviewerName": "Alan Montgomery", "helpful": [4, 4], "reviewText": "I needed a set of jumper cables for my new car and these had good reviews and were at a good price. They have been used a few times already and do what they are supposed to - no complaints there.What I will say is that 12 feet really isn't an ideal length. Sure, if you pull up front bumper to front bumper they are plenty long, but a lot of times you will be beside another car or can't get really close. Because of this, I would recommend something a little longer than 12'.Great brand - get 16' version though.", "overall": 5.0, "summary": "Work Well - Should Have Bought Longer Ones", "unixReviewTime": 1313539200, "reviewTime": "08 17, 2011"}
# created by asha pavan using python2.7
import json
import csv
data = []
f1 = csv.writer(open("test1.csv", "wb+"))
f1.writerow(['reviewerID','asin','reviewerName','helpful','reviewText','overall','summary','unixReviewTime','reviewTime'])
with open('/home/rakashi/Desktop/Automotive_5.json') as f:
for line in f:
#print line
line.replace('"','')
text = json.loads(line)
keys = text.keys()
print keys
if 'reviewerName' in text:
f1.writerow([text['reviewerID'],text['asin'],text['reviewerName'],text['helpful'],text['reviewText'],text['overall'],text['summary']])
else:
print 'This does not have a reviewerName entry'
#{"reviewerID": "A3F73SC1LY51OO", "asin": "B00002243X", "reviewerName": "Alan Montgomery", "helpful": [4, 4], "reviewText": "I needed a set of jumper cables for my new car and these had good reviews and were at a good price. They have been used a few times already and do what they are supposed to - no complaints there.What I will say is that 12 feet really isn't an ideal length. Sure, if you pull up front bumper to front bumper they are plenty long, but a lot of times you will be beside another car or can't get really close. Because of this, I would recommend something a little longer than 12'.Great brand - get 16' version though.", "overall": 5.0, "summary": "Work Well - Should Have Bought Longer Ones", "unixReviewTime": 1313539200, "reviewTime": "08 17, 2011"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment