【问题标题】:boto s3 - write a csv file using arrayboto s3 - 使用数组写入 csv 文件
【发布时间】:2018-03-13 14:48:24
【问题描述】:

我想将文件上传到 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)

它不接受数组,任何帮助我该如何实现?

【问题讨论】:

    标签: python csv amazon-s3 boto boto3


    【解决方案1】:

    首先,将数组变成多行字符串。

    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
    

    希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 2017-09-07
      • 1970-01-01
      • 2016-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多