如何使用python从S3存储桶中读取.txt文件并查看内容?

时间:2020-10-03 07:37:31

标签: python amazon-web-services amazon-s3

data = s3client.get_object(Bucket='', Key='')
data1 = data['Body']

data2 = json.load(data1)
data2 = json_normalize(data=data2)
data2 = sor.explode('header.head')
data2 = data2.to_dict('records')
data2 = json_normalize(data2)

我正在使用上面的json文件代码取消嵌套复杂的json和分析数据。如何执行相同的操作来流文本文件并查看内容。

data = s3client.get_object(Bucket='', Key='')
data1 = data['Body']
with open(data1) as sample: 
    for line in sample: 
        print(line.rstrip()) 

我遇到以下错误

TypeError: expected str, bytes or os.PathLike object, not StreamingBody

1 个答案:

答案 0 :(得分:1)

通常,您将使用iter_linesiter_chunks。例如:

data = s3client.get_object(Bucket='', Key='')

for line in data['Body'].iter_lines(): 
    print(line) 

请注意,line将是bytes,而不是str