S3选择CSV标头 [英] S3 Select CSV Headers
问题描述
我正在使用S3选择从S3存储桶读取csv文件并以CSV输出.在输出中,我仅看到行,而没有标题.如何获得包含标头的输出.
I am using S3 Select to read csv file from S3 Bucket and outputting as CSV. In the output I only see rows, but not headers. How do I get output with headers included.
import boto3
s3 = boto3.client('s3')
r = s3.select_object_content(
Bucket='demo_bucket',
Key='demo.csv',
ExpressionType='SQL',
Expression="select * from s3object s",
InputSerialization={'CSV': {"FileHeaderInfo": "Use"}},
OutputSerialization={'CSV': {}},
)
for event in r['Payload']:
if 'Records' in event:
records = event['Records']['Payload'].decode('utf-8')
print(records)
CSV
Name, Age, Status
Rob, 25, Single
Sam, 26, Married
s3select的输出
Output from s3select
Rob, 25, Single
Sam, 26, Married
推荐答案
更改InputSerialization={'CSV': {"FileHeaderInfo": "Use"}},
到 InputSerialization={'CSV': {"FileHeaderInfo": "NONE"}},
TO InputSerialization={'CSV': {"FileHeaderInfo": "NONE"}},
然后,它将打印完整的内容,包括标题.
Then, it will print full content, including the header.
说明:
FileHeaderInfo
接受"NONE | USE | IGNORE"之一.
FileHeaderInfo
accepts one of "NONE|USE|IGNORE".
使用NONE
选项而不是USE
,它还会打印标题,因为NONE
告诉您还需要标题来进行处理.
Use NONE
option rather then USE
, it will then print header as well, as NONE
tells that you need header as well for processing.
希望对您有帮助.
这篇关于S3选择CSV标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!