32 lines
930 B
Python
32 lines
930 B
Python
|
"""A conversion module for googletrans"""
|
||
|
import re
|
||
|
import json
|
||
|
|
||
|
def format_json(text):
|
||
|
# save state
|
||
|
states = []
|
||
|
for i, pos in enumerate(re.finditer('"', text)):
|
||
|
p = pos.start() + 1
|
||
|
if i % 2 == 0:
|
||
|
nxt = text.find('"', p)
|
||
|
states.append((p, text[p:nxt]))
|
||
|
|
||
|
# replace all weired characters in text
|
||
|
while text.find(',,') > -1:
|
||
|
text = text.replace(',,', ',null,')
|
||
|
while text.find('[,') > -1:
|
||
|
text = text.replace('[,', '[null,')
|
||
|
|
||
|
# recover state
|
||
|
for i, pos in enumerate(re.finditer('"', text)):
|
||
|
p = pos.start() + 1
|
||
|
if i % 2 == 0:
|
||
|
j = int(i / 2)
|
||
|
nxt = text.find('"', p)
|
||
|
# replacing a portion of a string
|
||
|
# use slicing to extract those parts of the original string to be kept
|
||
|
text = text[:p] + states[j][1] + text[nxt:]
|
||
|
|
||
|
converted = json.loads(text)
|
||
|
return converted
|