Mercurial > hg > dml-open-backendtools
view collection_analysis/tools/csv2json.py @ 0:e34cf1b6fe09 tip
commit
author | Daniel Wolff |
---|---|
date | Sat, 20 Feb 2016 18:14:24 +0100 |
parents | |
children |
line wrap: on
line source
# Part of DML (Digital Music Laboratory) # Copyright 2014-2015 Daniel Wolff, City University # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA #!/usr/bin/python # -*- coding: utf-8 -*- # this file converts data (csv) to the final web api json format. __author__="Daniel Wolff" __date__ ="$11-Feb-2015 16:32:09$" import sys import csv import json # global data format version dv=str(1) def getIdentifiers(): # collect collection and perspective id cid = raw_input("Please Input Collection Identifier:") pid = raw_input("Please Input Perspective / Analyisis Identifier:") # additional parameters params = raw_input("Input further parameters as string 'parameter1=value1_parameter2=value2...'") return (cid,pid,params) def makeUrl(cid,pid,params): #"getCollectionPerspective_pid=chordSequences_cid=jazz_limit=100.json" s = "_" if not params == '': url = s.join(['getCollectionPerspective','pid='+ pid,'cid=' + cid,params,"dv="+dv]) else: url = s.join(['getCollectionPerspective','pid='+ pid,'cid=' + cid,'dv='+dv]) return url # reads in any csv and returns a list of structure # time(float), data1, data2 ....data2 def read_csv(filein = ''): output = [] with open(filein, 'rb') as csvfile: contents = csv.reader(csvfile, delimiter=',', quotechar='"') for row in contents: output.append(row) return output # write data into object formatted and create json output def data2apijson(data,cid,pid,params): url = makeUrl(cid,pid,params) if not type(data)=='dict': obj = {"query":url, "result": {pid:data}} else: obj = {"query":url, "result": data} return (json.dumps(obj), url) # writes json to file def writejsonfile(jstring,url): f = open(url + ".json", "w"); f.write(jstring) f.close def data2json(data): # get further needed data such as url (cid,pid,params) = getIdentifiers() (jsondat,url) = data2apijson(data,cid,pid,params) # write to file writejsonfile(jsondat,url) #call this with csv input if __name__ == "__main__": # load data in file try: if len(sys.argv) < 2: csvfile = 'test.csv' else: csvfile = sys.argv[1] data = read_csv(csvfile) except: print "Please provide input csv file as command-line argument" quit() data2json(data)