S3选择CSV标头 [英] S3 Select CSV Headers

查看:151
本文介绍了S3选择CSV标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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.

这里是参考. https://boto3. amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.select_object_content

希望对您有帮助.

这篇关于S3选择CSV标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆