[英]Read csv file from S3 with Python DictReader
I am looking to write Python code that reads a csv file from S3 and then goes through the files line by line and uses the values in each line. 我正在寻找编写从S3读取csv文件,然后逐行浏览文件并使用每一行中的值的Python代码。 The example below is from realpython.org and I like how you can select the value in the row using the column header name like row["department"]. 下面的示例来自realpython.org,我喜欢如何使用列标题名称(如row [“ department”])在行中选择值。
I have tried to use csv.DictReader() with the csv file I get from my S3 bucket but it returns either an invalid argument
or TypeError: expected str, bytes or os.PathLike object, not StreamingBody
when I open the file with open(obj['Body'], mode='r') as csv_file:
我尝试将csv.DictReader()与从S3存储桶中获取的csv文件一起使用,但是它返回invalid argument
或TypeError: expected str, bytes or os.PathLike object, not StreamingBody
当我with open(obj['Body'], mode='r') as csv_file:
打开文件时TypeError: expected str, bytes or os.PathLike object, not StreamingBody
with open(obj['Body'], mode='r') as csv_file:
Does the code below work with csv files read from S3 buckets? 以下代码是否可用于从S3存储桶读取的csv文件?
import csv
with open('employee_birthday.txt', mode='r') as csv_file:
csv_reader = csv.DictReader(csv_file)
line_count = 0
for row in csv_reader:
if line_count == 0:
print(f'Column names are {", ".join(row)}')
line_count += 1
print(f'\t{row["name"]} works in the {row["department"]} department, and was born in {row["birthday month"]}.')
line_count += 1
print(f'Processed {line_count} lines.')
obj['Body']
is a StreamingBody
object, as the error message indicates. obj['Body']
是一个StreamingBody
对象,如错误消息所示。 Since it is a file-like object, you should pass it to DictReader
instead of opening it as the path to a file, but since it returns the stream in bytes and the DictReader
expects a text stream, you can wrap it in io.TextIOWrapper
instead: 由于它是一个类似于文件的对象,因此应将其传递给DictReader
而不是将其作为文件的路径打开,但是由于它以字节为单位返回流并且DictReader
需要文本流,因此可以将其包装在io.TextIOWrapper
代替:
import io
csv_reader = csv.DictReader(io.TextIOWrapper(obj['Body']))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.