我正在尝试编写一个函数,从我的数据库中获取数据,该函数已经正常工作。
这是我在添加实际记录之前的标题代码:
with open('csv_template.csv', 'a') as template_file:
#declares the variable template_writer ready for appending
template_writer = csv.writer(template_file, delimiter=',')
#appends the column names of the excel table prior to adding the actual physical data
template_writer.writerow(['Arrangement_ID','Quantity','Cost'])
#closes the file after appending
template_file.close()
Run Code Online (Sandbox Code Playgroud)
这是我的记录代码,它包含在 while 循环中,并且是将两个脚本分开的主要原因。
with open('csv_template.csv', 'a') as template_file:
#declares the variable template_writer ready for appending
template_writer = csv.writer(template_file, delimiter=',')
#appends the data of the current fetched values of the sql statement within the while loop to the csv file
template_writer.writerow([transactionWordData[0],transactionWordData[1],transactionWordData[2]]) …Run Code Online (Sandbox Code Playgroud)