简体   繁体   中英

Python, writing CSV file has extra blank rows

I have a piece of python code which is supposed to open (or create) a CSV file and append a new row to the end of it. However, there is an extra blank line added between each entry. Is there a way to avoid this?

I have around 500 instances of the script which all access the same file, though (in theory) they should access the file at different times.

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()

Problem answered through comments. The issue was using the wb mode not ab mode.

“ b”选项对我有用,请查看API文档: https : //docs.python.org/2/library/functions.html? highlight = open# open

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM