-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathgetTaxi-2.py
More file actions
84 lines (73 loc) · 1.97 KB
/
getTaxi-2.py
File metadata and controls
84 lines (73 loc) · 1.97 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/python
import sys, string
strip_out = '-:\"\' ' # strip out quotes, white-spaces, colon and dash
# for test purpose
yaml_temp = '''---
foo: "bar"
baz:
- "qux"
- "quxx"
corge: null
grault: 1
garply: true
waldo: "false"
fred: "undefined"
emptyArray: []
emptyObject: {}
emptyString: ""
strakovich:
iliya:
- 328800065
- 1975'''
## open an URL
#response = urllib2.urlopen('https://gist.githubusercontent.com/dmitrypa/ee74d809505bf6c69f6f/raw/01e84c2bcb791153346b57cca4e4d2751b8ca111/example.yml')
#yaml_temp = response.read()
# convert string to list delimited by new-lines
yaml = yaml_temp.split('\n')
# check if it's YAML
if yaml.pop(0) != '---':
sys.exit()
# wipe out comments and empty lines
yaml = []
for i in yaml_temp:
if not (i == '' or i.startswith('#')):
yaml.append(i)
del yaml_temp
json = []
for index in range(len(yaml)):
item = yaml[index].split(':')
key = item[0].strip(strip_out)
# add list
if len(item) == 1:
json[anchor] += '"' + key + '"' + ', '
# add key
elif len(item) == 2 and len(item[1]) == 0:
try:
json[anchor] += '"' + key + '"' + ':'
except:
json.append('"' + key + '"' + ':')
anchor = len(json) - 1
# add whole item
else:
val = item[1].strip(strip_out)
val = '"' + val + '"'
json.append('"' + key + '"' + ':' + val)
try:
del anchor
except:
continue
for ind in range(len(json)):
if json[ind].endswith(', '):
lst = json[ind].split(':')
mod_list = lst[:-1]
lst[-1] = '[' + lst[-1].rstrip(', ') + ']'
json[ind] = ':'.join(lst)
# elif json[ind].count(':') >= 2:
# print json[ind]
# Strings should be wrapped in double quotes
trantab = string.maketrans("'", ' ') # create translation table
json = str(json).translate(trantab)
print '{ ' + json.strip('[ ]') + '] }'
## print out to the file
#with open('gett.json', 'w') as file:
# file.write(json)