55from __future__ import unicode_literals
66
77import json
8-
98from copy import deepcopy
109from itertools import groupby
1110from operator import itemgetter
@@ -26,7 +25,6 @@ def gen_leaf(root, path=None):
2625 if path is None :
2726 path = []
2827
29- # the leaf
3028 if not isinstance (root , (dict , list )) or not root :
3129 leaf = (path , root )
3230 yield leaf
@@ -36,7 +34,6 @@ def gen_leaf(root, path=None):
3634 items = root .iteritems ()
3735 else :
3836 items = root .items ()
39-
4037 else :
4138 items = enumerate (root )
4239
@@ -47,14 +44,17 @@ def gen_leaf(root, path=None):
4744 yield leaf
4845
4946
50- def is_array (keys , ensure_str = True ):
51- copy_keys = list (deepcopy (keys ))
52- int_keys = list (range (len (copy_keys )))
53- if copy_keys == int_keys :
47+ def is_array_index (keys , enable_str = True ):
48+ keys = list (deepcopy (keys ))
49+ # 不强调有序
50+ key_map = {key : True for key in keys }
51+ int_keys = range (len (keys ))
52+
53+ if all (key in key_map for key in int_keys ):
5454 return True
55- if ensure_str :
56- str_keys = list ( map ( str , int_keys ))
57- if copy_keys == str_keys :
55+
56+ if enable_str :
57+ if all ( str ( key ) in key_map for key in int_keys ) :
5858 return True
5959 return False
6060
@@ -84,7 +84,7 @@ def from_leaf(leafs):
8484 child .append ((head , _child ))
8585
8686 child_keys = map (_get_head , child )
87- if is_array (child_keys ):
87+ if is_array_index (child_keys ):
8888 child .sort (key = lambda x : int (x [0 ]))
8989 return list (map (_get_leaf , child ))
9090
@@ -140,6 +140,7 @@ def convert_json(fin, fout, func, separator=".", safe=False, json_array=False):
140140
141141 # default: read json objects from each line
142142 def gen_objs ():
143+ print (type (fin ))
143144 for line in fin :
144145 obj = json .loads (line )
145146 yield obj
@@ -161,6 +162,7 @@ def gen_objs_from_array():
161162 content = json .dumps (new , ensure_ascii = False )
162163 if PY2 :
163164 fout .write (content .encode ('utf-8' ))
165+ fout .write ('\n ' .encode ('utf-8' ))
164166 else :
165167 fout .write (content )
166- fout .write (str ( '\n ' ) )
168+ fout .write ('\n ' )
0 commit comments