简体   繁体   中英

Writing a csv file with commas and double quotes in a cell in Python

I am trying to support double quotes and commas in a cell when righting a csv file in Python. heres my code for writing the csv file.

def writecsv(filename):
    if(re.match('.csv$', filename)):
    filename = re.sub('\w+', filename)

    try:
        csvfile = open(filename, 'w', newline='')
        csvwriter = csv.writer(csvfile, delimiter=',')
        for row in spreadsheet[3]:
            csvwriter.writerow(row)
            csvfile.close()
    except:
        print('The file "'+filename+'" did not save correctly please try again')

right now if there is a double quote it ends the cell so how would I skip one of the ending double quotes? each cell is contained in a list.

查看CSV库https://docs.python.org/2/library/csv.html的文档,看来您需要定义一个方言对象,该对象可以定义应如何处理引号并传递交给CSV编写者。

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