Python CSV NamedTemporaryFile未正确保存
我正在尝试使用Python的csv和tempfile工具创建一个csv文件。我已经声明如下:Python CSV NamedTemporaryFile未正确保存,python,csv,temporary-files,Python,Csv,Temporary Files,我正在尝试使用Python的csv和tempfile工具创建一个csv文件。我已经声明如下: csvattachment = tempfile.NamedTemporaryFile(suffix='.csv', prefix=('student_' + studentID), delete=False) with open(csvattachment.name, 'w+') as csvfile: filewriter = csv.writer(csvfile, delimiter=',
csvattachment = tempfile.NamedTemporaryFile(suffix='.csv', prefix=('student_' + studentID), delete=False)
with open(csvattachment.name, 'w+') as csvfile:
filewriter = csv.writer(csvfile, delimiter=',')
filewriter.writerow([ #WRITE CONTENT HERE])
之后我要做的是附加这个文件并发送出去。问题是附件名不是“student_1736823.csv”,而是更难看的名称,比如
名为temporaryfile()的类已经返回了一个打开的文件,您不必重新打开它
with tempfile.NamedTemporaryFile(suffix='.csv', prefix=('student_' + studentID),
delete=False, mode='w+') as csvfile:
filewriter = csv.writer(csvfile, delimiter=',')
filewriter.writerow([ #WRITE CONTENT HERE])
NamedTemporaryFile()
类已经返回一个打开的文件,您不必重新打开它
with tempfile.NamedTemporaryFile(suffix='.csv', prefix=('student_' + studentID),
delete=False, mode='w+') as csvfile:
filewriter = csv.writer(csvfile, delimiter=',')
filewriter.writerow([ #WRITE CONTENT HERE])