python - boto s3 - 使用数组写入一个 csv 文件

标签 python csv amazon-s3 boto boto3

我想上传一个文件到 s3 而不在我的本地系统上写入文件,所以我使用 boto 库的 set_contents_from_string。我可以将此文件上传回 s3。

但是如何使用 boto 库将数组写入 csv 文件。

arrlist = [[2, 'jack'], [3, 'john'], [4, 'robert']]
file_name = "test.csv" 
k = Key(bucket, file_name)
k.set_contents_from_string(arrlist)

它不接受数组,有什么帮助我怎样才能做到这一点?

最佳答案

首先,将数组转为多行字符串。

arrlist = [[2, 'jack'], [3, 'john'], [4, 'robert']]

i = 0
multiLine = ""

for item in arrlist:
    newLine = ','.join(map(str, item))

    if (i > 0) {
        multiLine = multiLine + '\n'
    }
    multiLine = multiLine + newLine
    i++

file_name = "test.csv" 
k = Key(bucket, file_name)
k.set_contents_from_string(multiLine)

写入文件的输出multiLine如下:

2,jack
3,john
4,robert

希望对您有所帮助!

关于python - boto s3 - 使用数组写入一个 csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49249564/

相关文章:

python - 为什么我的第二个函数没有返回任何内容?

python - 如何将嵌套 xml(与子项同名)解析为 CSV?

python - Boto s3 获取元数据

scala - 如何在 EMR 中运行的 MapReduce 作业中转义 JSON 禁止八进制前缀异常

python - .bashrc 中的 conda 环境路径

python - 在Windows 7上用Python快速获取屏幕上某些像素点的颜色

c++ - Python sprintf 和 memset 操作

ruby-on-rails - ruby /Rails : Best way to loop through a csv and set flag when new person is found

amazon-web-services - AWS 创建角色 - 具有禁止字段

python - 在视频 ffmpeg 批处理脚本中显示文件名