0

我有用python编写的简单代码。在写入excel文件时。我发现每次都会添加额外的行。如何跳过每次添加的空行。并在excel文件中一个接一个地打印数据

import csv
from datetime import datetime
import time

filename = 'testformat.csv'
fields = ['SLNo', 'Date', 'Time', 'RData', 'BData', 'GData', 'IRData']
date_format = datetime.now().strftime('%Y/%m/%d')
current_time = datetime.now().strftime('%I:%M:%S,%f')

def main():
    with open(filename, 'w') as csvfile:
        csvwriter = csv.writer(csvfile)
        csvwriter.writerow(fields)
        for i in range(30):
            csvwriter.writerow([i, date_format, current_time])
if __name__ == '__main__':
    main()
4

1 回答 1

1

您需要的已经在这里:https ://stackoverflow.com/a/3191811/18081892

你必须使用:

with open(filename, 'w', newline='', encoding='utf-8') as csvfile:
于 2022-02-03T21:53:24.763 回答