def json2csv_entities(tweets_file, outfile, main_fields, entity_type, entity_fields, encoding='utf8', errors='replace', gzip_compress=False):
 (writer, outf) = outf_writer_compat(outfile, encoding, errors, gzip_compress)
 header = get_header_field_list(main_fields, entity_type, entity_fields)
 writer.writerow(header)
 for line in tweets_file:
  tweet = json.loads(line)
  if _is_composed_key(entity_type):
   (key, value) = _get_key_value_composed(entity_type)
   object_json = _get_entity_recursive(tweet, key)
   if (not object_json):
    continue
   object_fields = extract_fields(object_json, main_fields)
   items = _get_entity_recursive(object_json, value)
   _write_to_file(object_fields, items, entity_fields, writer)
  else:
   tweet_fields = extract_fields(tweet, main_fields)
   items = _get_entity_recursive(tweet, entity_type)
   _write_to_file(tweet_fields, items, entity_fields, writer)
 outf.close()

def json2csv_entities ( tweets_file , outfile , main_fields , entity_type , entity_fields , encoding = 'utf8' , errors = 'replace' , gzip_compress = False ) : ( writer , outf ) = outf_writer_compat ( outfile , encoding , errors , gzip_compress ) header = get_header_field_list ( main_fields , entity_type , entity_fields ) writer . writerow ( header ) for line in tweets_file : tweet = json . loads ( line ) if _is_composed_key ( entity_type ) : ( key , value ) = _get_key_value_composed ( entity_type ) object_json = _get_entity_recursive ( tweet , key ) if ( not object_json ) : continue object_fields = extract_fields ( object_json , main_fields ) items = _get_entity_recursive ( object_json , value ) _write_to_file ( object_fields , items , entity_fields , writer ) else : tweet_fields = extract_fields ( tweet , main_fields ) items = _get_entity_recursive ( tweet , entity_type ) _write_to_file ( tweet_fields , items , entity_fields , writer ) outf . close ( )
extract selected fields from a file of line-separated json tweets and write to a file in csv format .