Created
November 20, 2011 23:54
-
-
Save versae/1381199 to your computer and use it in GitHub Desktop.
Convert JSON files from ViralHeat API into CSV's files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*-*- coding: utf-8 -*- | |
import codecs | |
import csv | |
from StringIO import StringIO | |
from json import loads | |
# Taken from http://docs.python.org/library/csv.html#csv-examples | |
class UnicodeWriter(object): | |
""" | |
A CSV writer which will write rows to CSV file "f", | |
which is encoded in the given encoding. | |
""" | |
def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds): | |
# Redirect output to a queue | |
self.queue = StringIO() | |
self.writer = csv.writer(self.queue, dialect=dialect, **kwds) | |
self.stream = f | |
self.encoder = codecs.getincrementalencoder(encoding)() | |
def writerow(self, row): | |
self.writer.writerow([unicode(s).encode("utf-8") for s in row]) | |
# Fetch UTF-8 output from the queue ... | |
data = self.queue.getvalue() | |
data = data.decode("utf-8") | |
# ... and reencode it into the target encoding | |
data = self.encoder.encode(data) | |
# write to the target stream | |
self.stream.write(data) | |
# empty queue | |
self.queue.truncate(0) | |
def writerows(self, rows): | |
for row in rows: | |
self.writerow(row) | |
def main(): | |
parties = ["pp", "psoe", "iu", "cc", "equo", "esquerra", "upyd", "geroabai"] | |
lines = {} | |
for party in parties: | |
lines[party] = [] | |
csv_file = open("%s.csv" % party, "w") | |
writer = UnicodeWriter(csv_file) | |
num = 1 | |
for line in open("programa-%s.json" % party, "r"): | |
num += 1 | |
if len(line.strip()) > 4: | |
l = loads(line.decode("utf-8")) | |
try: | |
int(l["text"]) | |
except: | |
if "error" not in l: | |
if len(l["text"].strip()) > 4: | |
if l["mood"].lower() == "negative": | |
writer.writerow([party, l["mood"], -l["prob"], | |
l["text"]]) | |
lines[party].append(-l["prob"]) | |
else: | |
writer.writerow([party, l["mood"], l["prob"], | |
l["text"]]) | |
lines[party].append(l["prob"]) | |
csv_file.close() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment