CSV文件格式不适用于GCP BlobWriter

gwo2fgha  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(85)

我有相同的数据作为字典列表,并希望在一个CSV文件导入这些在GCP桶。
我将这些对象导入为流:

blob = defaults.bucket.blob(filename)
csv_writer = BlobWriter(blob, content_type="text/csv")

for data in result.get('events'):
    _source = data.get('_source', {})
    csv_writer.write(json.dumps(_source, indent=2).encode('utf-8'))

字符串
其中_source是dict。
CSV文件中的实际结果是JSON格式,而不是CSV。
举例来说:

{           
  'key':'value'     
}{          
  'key':'value'         
}{

gudnpqoy

gudnpqoy1#

import csv

blob = defaults.bucket.blob(filename)
csv_writer = BlobWriter(blob, content_type="text/csv")

# Assuming '_source' is a dictionary and result.get('events') is a list of dictionaries
field_names = ['key1', 'key2', 'key3']  # Replace with actual keys in your '_source' dictionaries

# Write CSV header
csv_writer.write(','.join(field_names).encode('utf-8') + b'\n')

for data in result.get('events'):
    _source = data.get('_source', {})
    
    # Convert each dictionary to a CSV row
    csv_row = [str(_source.get(key, '')) for key in field_names]
    
    # Write CSV row to the file
    csv_writer.write(','.join(csv_row).encode('utf-8') + b'\n')

字符串

相关问题