#NEWLINE# @ append . register ( JSONLines , Iterator ) #NEWLINE# def iterator_to_json_lines( j , seq , dshape = None , encoding = 'utf-8' , **kwargs ) : #NEWLINE# #INDENT# row = next ( seq ) #NEWLINE# seq = concat ( [ [ row ] , seq ] ) #NEWLINE# if ( ( not isinstance ( row , ( dict , str ) ) ) and isinstance ( row , Iterable ) ) : #NEWLINE# #INDENT# seq = tuples_to_records ( dshape , seq ) #NEWLINE# #UNINDENT# lines = ( json . dumps ( item , default = json_dumps ) for item in seq ) #NEWLINE# if ( j . path . split ( os . path . extsep ) [ ( - 1 ) ] == 'gz' ) : #NEWLINE# #INDENT# f = gzip . open ( j . path , 'ab' ) #NEWLINE# lines2 = ( line . encode ( encoding ) for line in lines ) #NEWLINE# endl = b'\n' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# f = open ( j . path , 'a' ) #NEWLINE# lines2 = lines #NEWLINE# endl = '\n' #NEWLINE# #UNINDENT# for line in lines2 : #NEWLINE# #INDENT# f . write ( lines2 ) #NEWLINE# f . write ( endl ) #NEWLINE# #UNINDENT# f . close ( ) #NEWLINE# return j