@@ -198,12 +198,13 @@ def read_jsonline_lazy(filename, encoding=_ENCODING_UTF8, default=None):
198
198
file .close ()
199
199
200
200
201
- def write_jsonline (filename , items , encoding = _ENCODING_UTF8 ):
201
+ def write_jsonline (filename , items , encoding = _ENCODING_UTF8 , serialize_method = None ):
202
202
"""
203
203
write items to file with json line format
204
204
:param filename: destination file path
205
205
:param items: items to be saved line by line
206
206
:param encoding: file encoding
207
+ :param serialize_method: serialization method to process object
207
208
:return: None
208
209
"""
209
210
if isinstance (items , str ):
@@ -216,7 +217,7 @@ def write_jsonline(filename, items, encoding=_ENCODING_UTF8):
216
217
raise TypeError ('items can\' t be iterable' )
217
218
file = open (filename , 'w' , encoding = encoding )
218
219
for item in items :
219
- file .write (json .dumps (item , ensure_ascii = False ) + '\n ' )
220
+ file .write (json .dumps (item , ensure_ascii = False , default = serialize_method ) + '\n ' )
220
221
file .close ()
221
222
222
223
@@ -274,29 +275,31 @@ def append_lines(filename, lines, remove_file=False, encoding=_ENCODING_UTF8):
274
275
append_line (filename , line , encoding )
275
276
276
277
277
- def append_jsonline (filename , item , encoding = _ENCODING_UTF8 ):
278
+ def append_jsonline (filename , item , encoding = _ENCODING_UTF8 , serialize_method = None ):
278
279
"""
279
280
append item as a line of json string to file
280
281
:param filename: destination file
281
282
:param item: item to be saved
282
283
:param encoding: file encoding
284
+ :param serialize_method: serialization method to process object
283
285
:return: None
284
286
"""
285
287
with open (filename , 'a' , encoding = encoding ) as f :
286
- f .write (json .dumps (item , ensure_ascii = False ) + '\n ' )
288
+ f .write (json .dumps (item , ensure_ascii = False , default = serialize_method ) + '\n ' )
287
289
288
290
289
- def append_jsonlines (filename , items , encoding = _ENCODING_UTF8 ):
291
+ def append_jsonlines (filename , items , encoding = _ENCODING_UTF8 , serialize_method = None ):
290
292
"""
291
293
append item as some lines of json string to file
292
294
:param filename: destination file
293
295
:param items: items to be saved
294
296
:param encoding: file encoding
297
+ :param serialize_method: serialization method to process object
295
298
:return: None
296
299
"""
297
300
with open (filename , 'a' , encoding = encoding ) as f :
298
301
for item in items :
299
- f .write (json .dumps (item , ensure_ascii = False ) + '\n ' )
302
+ f .write (json .dumps (item , ensure_ascii = False , default = serialize_method ) + '\n ' )
300
303
301
304
302
305
class __BaseFile (object ):
0 commit comments