Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python CSV NamedTemporaryFile未正确保存_Python_Csv_Temporary Files - Fatal编程技术网

Python CSV NamedTemporaryFile未正确保存

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=',

我正在尝试使用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=',')
    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])