我尝试使用以下代码从存储在 S# 存储桶中的 CSV 中获取记录子集:
s3 = boto3.client('s3')
bucket = bucket
file_name = file
sql_stmt = """SELECT S.* FROM s3object S LIMIT 10"""
req = s3.select_object_content(
Bucket=bucket,
Key=file,
ExpressionType='SQL',
Expression=sql_stmt,
InputSerialization = {'CSV': {'FileHeaderInfo': 'USE'}},
OutputSerialization = {'CSV': {}},
)
records = []
for event in req['Payload']:
if 'Records' in event:
records.append(event['Records']['Payload'])
elif 'Stats' in event:
stats = event['Stats']['Details']
file_str = ''.join(r.decode('utf-8') for r in records)
select_df = pd.read_csv(StringIO(file_str))
df = pd.DataFrame(select_df)
print(df)
这成功地产生了记录,但错过了标头。
我在这里读到S3 选择 CSV 标头 https://stackoverflow.com/questions/50848573/s3-select-csv-headersS3 Select 根本不产生标头。那么,是否可以通过其他方式检索 S3 中 CSV 文件的标头?
Change InputSerialization={'CSV': {"FileHeaderInfo": "Use"}},
TO InputSerialization={'CSV': {"FileHeaderInfo": "NONE"}},
然后,它将打印完整的内容,包括header
.
解释:
FileHeaderInfo
接受“无”或“使用”或“忽略”之一。
Use NONE
选项而不是USE
,然后它将打印header
也NONE
告诉你需要header
以及对于processing
.
这里是参考。https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.select_object_content https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.select_object_content
我希望它有帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)