You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从S3存储的Athena查询结果CSV中提取instanceId列

两种场景下提取instanceId数组的解决方案

场景1:从S3存储的CSV文件中提取

先修正原读取函数的行分割逻辑(原split()会按任意空白拆分,破坏行结构),再过滤表头、提取目标列并清理多余引号:

def read_instanceids(path):
    s3 = boto3.resource('s3')
    bucket = s3.Bucket('aws-athena-query-results-mybucket-us-east-1')
    obj = bucket.Object(key=path)
    response = obj.get()
    # 按换行符分割每行,过滤空行
    lines = [line.strip() for line in response['Body'].read().decode('utf-8').split('\n') if line.strip()]
    instance_ids = []
    # 跳过表头,遍历数据行
    for line in lines[1:]:
        # 按逗号拆分列(你的场景中instanceId不含逗号,此方法可行)
        parts = line.split(',')
        if len(parts) >= 2:
            # 清除所有包裹的引号(比如把"""i-0aa1f4dd"""转成i-0aa1f4dd)
            instance_id = parts[1].strip('"')
            instance_ids.append(instance_id)
    return instance_ids

场景2:使用Athena get_query_results接口提取

直接遍历返回结果,跳过表头行,提取目标字段并清理引号:

# 假设query_results是get_query_results接口返回的结果列表
instance_ids = []
# 跳过第一个表头元素
for row in query_results[1:]:
    # 取第二列(index=1)的VarCharValue值
    raw_id = row['Data'][1]['VarCharValue']
    # 清除字段前后的引号(比如把"i-053090803"转成i-053090803)
    clean_id = raw_id.strip('"')
    instance_ids.append(clean_id)

内容的提问来源于stack exchange,提问作者D kashyap

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 08:30:24