【问题标题】:Writing string output to csv file. Getting error when working with temporary file将字符串输出写入 csv 文件。使用临时文件时出错
【发布时间】:2021-12-12 06:37:45
【问题描述】:

我正在遍历从 S3 提取的 excel 文件。我想将此数据附加到一个文件中。数据不足以超过 lambda 内存限制,因此我将其保存到变量中,然后将字符串转换为我希望上传到 S3 的 csv 文件。当我在本地运行此代码的变体时,它运行良好,不确定将其转换为 AWS 时出了什么问题。

import csv
import boto3
import urllib3
import tempfile

s3 = boto3.client('s3')
bucket = os.environ['S3_BUCKET']
http = urllib3.PoolManager()

def lambda_handler(event, context):
 file = readS3('example.xlsx') # load file with Boto3
 latest_scan = openpyxl.load_workbook(io.BytesIO(file), data_only=True)
 sh = latest_scan.active


 a = []
 for row in sh['A']:
    r5 = http.request(
        'GET',
        'https://example.com/api/' + str(row.value),
        headers={
            'Accept': 'text/csv'
        }
    )
    a.append(r5.data.decode('utf-8'))
 s = ''.join(a)
 temp = tempfile.TemporaryFile(mode='w+', suffix='.csv')
 with open(temp, 'w', encoding="utf-8") as f:
    for line in s:
        f.write(line)
 temp.seek(0)
 s3.put_object(temp, Bucket = bucket, Key = 'test.csv')
 temp.close() 

我明白了:

 "errorMessage": "expected str, bytes or os.PathLike object, not _io.TextIOWrapper",
 "errorType": "TypeError",
  "stackTrace": [
     "  File \"/var/task/lambda_function.py\", line in lambda_handler\n    with open(temp, 
       'w', encoding=\"utf-8\") as f:\n"
  ]

【问题讨论】:

  • 您无需致电open()tempfile.TemporaryFile() 返回一个打开的文件对象,而不是文件名。

标签: python amazon-s3 aws-lambda


【解决方案1】:

tempfile.TemporaryFile() 打开文件,它不返回文件名。因此,只需将其分配给 f

with tempfile.TemporaryFile(mode='w+', suffix='.csv', encoding="utf-8") as f:

【讨论】:

  • 实施了删除初始错误的更改。虽然无法将临时文件上传到 S3。有推荐的解决方案吗?我尝试了许多变体并不断遇到不同的错误。
  • 确保在with 块内进行上传,这样文件就不会被删除。见stackoverflow.com/questions/69730782/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多