Python,写CSV文件有额外的空行

2024-10-05 14:25:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一段python代码,它应该打开(或创建)一个CSV文件,并在它的末尾追加一个新行。但是,在每个条目之间添加了一个额外的空行。有没有办法避免这种情况?在

我有大约500个脚本实例,它们都访问同一个文件,尽管(理论上)它们应该在不同的时间访问该文件。在

def writeBest(fileName, savePath, data):

    # Create a filename
    name = "BEST_" + fileName + ".csv"

    # Create the complete filename including the absolute path 
    completePath = os.path.join(savePath, fileName)

    # Check if directory exists
    if not os.path.exists(completePath):
        os.makedirs(completePath)

    completeName = os.path.join(completePath, name)

    # Write the data to a file
    theFile = open(completeName, 'wb')
    writer = csv.writer(theFile, quoting=csv.QUOTE_ALL)
    writer.writerow(data)

    # Close the file
    theFile.close()

Tags: 文件csvthepathnamedataoscreate