Mercurial > hg > dml-open-backendtools
comparison collection_analysis/tools/csv2json.py @ 0:e34cf1b6fe09 tip
commit
author | Daniel Wolff |
---|---|
date | Sat, 20 Feb 2016 18:14:24 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e34cf1b6fe09 |
---|---|
1 # Part of DML (Digital Music Laboratory) | |
2 # Copyright 2014-2015 Daniel Wolff, City University | |
3 | |
4 # This program is free software; you can redistribute it and/or | |
5 # modify it under the terms of the GNU General Public License | |
6 # as published by the Free Software Foundation; either version 2 | |
7 # of the License, or (at your option) any later version. | |
8 # | |
9 # This program is distributed in the hope that it will be useful, | |
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 # GNU General Public License for more details. | |
13 # | |
14 # You should have received a copy of the GNU General Public | |
15 # License along with this library; if not, write to the Free Software | |
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 | |
18 #!/usr/bin/python | |
19 # -*- coding: utf-8 -*- | |
20 | |
21 # this file converts data (csv) to the final web api json format. | |
22 | |
23 __author__="Daniel Wolff" | |
24 __date__ ="$11-Feb-2015 16:32:09$" | |
25 | |
26 import sys | |
27 import csv | |
28 import json | |
29 | |
30 | |
31 # global data format version | |
32 dv=str(1) | |
33 | |
34 | |
35 def getIdentifiers(): | |
36 # collect collection and perspective id | |
37 cid = raw_input("Please Input Collection Identifier:") | |
38 pid = raw_input("Please Input Perspective / Analyisis Identifier:") | |
39 | |
40 # additional parameters | |
41 params = raw_input("Input further parameters as string 'parameter1=value1_parameter2=value2...'") | |
42 return (cid,pid,params) | |
43 | |
44 def makeUrl(cid,pid,params): | |
45 #"getCollectionPerspective_pid=chordSequences_cid=jazz_limit=100.json" | |
46 s = "_" | |
47 if not params == '': | |
48 url = s.join(['getCollectionPerspective','pid='+ pid,'cid=' + cid,params,"dv="+dv]) | |
49 else: | |
50 url = s.join(['getCollectionPerspective','pid='+ pid,'cid=' + cid,'dv='+dv]) | |
51 | |
52 return url | |
53 | |
54 | |
55 # reads in any csv and returns a list of structure | |
56 # time(float), data1, data2 ....data2 | |
57 def read_csv(filein = ''): | |
58 output = [] | |
59 with open(filein, 'rb') as csvfile: | |
60 contents = csv.reader(csvfile, delimiter=',', quotechar='"') | |
61 for row in contents: | |
62 output.append(row) | |
63 return output | |
64 | |
65 # write data into object formatted and create json output | |
66 def data2apijson(data,cid,pid,params): | |
67 url = makeUrl(cid,pid,params) | |
68 if not type(data)=='dict': | |
69 obj = {"query":url, "result": {pid:data}} | |
70 else: | |
71 obj = {"query":url, "result": data} | |
72 return (json.dumps(obj), url) | |
73 | |
74 # writes json to file | |
75 def writejsonfile(jstring,url): | |
76 f = open(url + ".json", "w"); | |
77 f.write(jstring) | |
78 f.close | |
79 | |
80 def data2json(data): | |
81 # get further needed data such as url | |
82 (cid,pid,params) = getIdentifiers() | |
83 (jsondat,url) = data2apijson(data,cid,pid,params) | |
84 | |
85 # write to file | |
86 writejsonfile(jsondat,url) | |
87 | |
88 #call this with csv input | |
89 if __name__ == "__main__": | |
90 # load data in file | |
91 try: | |
92 if len(sys.argv) < 2: | |
93 csvfile = 'test.csv' | |
94 else: | |
95 csvfile = sys.argv[1] | |
96 data = read_csv(csvfile) | |
97 except: | |
98 print "Please provide input csv file as command-line argument" | |
99 quit() | |
100 | |
101 data2json(data) |