【问题标题】:Writing a csv file with commas and double quotes in a cell in Python在 Python 中的单元格中编写带有逗号和双引号的 csv 文件
【发布时间】:2015-01-05 12:48:41
【问题描述】:

在 Python 中纠正 csv 文件时,我试图在单元格中支持双引号和逗号。这是我编写 csv 文件的代码。

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

现在如果有一个双引号,它会结束单元格,那么我将如何跳过一个结束双引号?每个单元格都包含在一个列表中。

【问题讨论】:

  • 什么是spreadsheet

标签: python csv


【解决方案1】:

查看 CSV 库 https://docs.python.org/2/library/csv.html 的文档,您似乎需要定义一个方言对象,它允许您定义应如何处理引号,并将其传递给 CSV 编写器。

【讨论】:

    猜你喜欢
    • 2017-09-28
    • 2022-08-06
    • 1970-01-01
    • 2019-03-28
    • 2011-03-29
    • 1970-01-01
    • 2014-02-26
    • 1970-01-01
    相关资源
    最近更新 更多