-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv2json.py
46 lines (40 loc) · 996 Bytes
/
csv2json.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import csv
import json
haplolist = []
seqdic = {}
with open('output.csv', 'r') as f:
reader = csv.reader(f)
for row in reader:
if row == ['0'] :
break
haplolist.append(row)
for row in reader:
if row == ['0'] :
break
seqdic[row[0]]=row[1];
f = open('output.json', 'w')
for haplo in haplolist:
seqstr = ""
for hid in haplo:
seqstr += seqdic[hid]
result = {}
result['sequence'] = seqstr
result['name'] = 'hoge'
result['path'] = {}
result['path']['name'] = 'fuga'
result['path']['mapping'] = []
rank = 1
for hid in haplo:
mappingdict = {}
mappingdict['position'] = {}
mappingdict['position']['node_id'] = hid
mappingdict['edit'] = []
lendict = {}
lendict['from_length'] = len(seqdic[hid])
lendict['to_length'] = len(seqdic[hid])
mappingdict['edit'].append(lendict)
mappingdict['rank'] = str(rank)
result['path']['mapping'].append(mappingdict)
rank += 1
json.dump(result, f)
f.write("\n")