csv writer 在每一行周围加上引号

Posted

技术标签:

【中文标题】csv writer 在每一行周围加上引号【英文标题】:csv writer puts quotes around each row 【发布时间】:2015-02-17 12:55:06 【问题描述】:

我正在尝试将多个相同格式的 csv 文件合并为一个。

merge_list = glob.glob(gndlbsum+"*gndlbsum.csv")
filewriter_lbsum = target_dir+"gndlbsum_master.csv"

#get the list of csv files and set the output file
counter=0
for file in merge_list:
    with open(file,"rU") as csv_file:
        filereader = csv.reader(csv_file)
        with open(filewriter_lbsum,"a") as f:
            writer = csv.writer(f, delimiter = "|")
            #check to see if it's the first file, if it is, add header,
            #otherwise skip first row
            if counter<1:
                for row in filereader:
                    writer.writerow(row)
                    counter+=1
            else:
                header = next(filereader,None)
                for row in filereader:
                    writer.writerow(row)

当我这样做时,输出 csv 中的每一行都完全用双引号括起来,我尝试使用list.append(row),但没有区别,因为该行用双引号括起来。有没有办法避免这种情况?

编辑:

这里是源文件的示例:

COL1|COL2|COL3
1|2|3
4|5|6

还有输出:

"COL1|COL2|COL3"
"1|2|3"
"4|5|6"

【问题讨论】:

【参考方案1】:

csv 模块在每行的末尾自动添加双引号。在不确切知道您的 csv 文件长什么样的情况下,请尝试以下操作:

merge_list = glob.glob(gndlbsum+"*gndlbsum.csv")
filewriter_lbsum = target_dir+"gndlbsum_master.csv"
#get the list of csv files and set the output file
counter=0
for file in merge_list:
    with open(file,"rU") as csv_file:
        filereader = csv.reader(csv_file, skipinitialspace=True)
        with open(filewriter_lbsum,"a") as f:
            writer = csv.writer(f,delimiter = "|", quoting=csv.QUOTE_NONE)
            #check to see if it's the first file, if it is, add header, 
            #otherwise skip first row
            if counter<1:
                for row in filereader:
                    writer.writerow(row)
                    counter+=1
            else:
                header = next(filereader,None)
                for row in filereader:
                    writer.writerow(row)

【讨论】:

所有源 csv 文件都有标题,它们是 |分隔,并且没有字符串值,只有数字,即使有,也没有在源代码中用引号引起来。这是一个例子:来源:COL1|COL2|COL3 1|2|3 4|5|6 输出:“COL1|COL2|COL3”“1|2|3”“4|5|6”我试过你的例子,但它返回一个空文件 csv 编写器不会自动在每行末尾添加双引号。【参考方案2】:

我认为这将消除由于没有告诉正在创建的csv.readers 输入文件中的分隔符是"|" 字符而不是默认的"," 字符而引起的引号。

merge_list = glob.glob(gndlbsum + "*gndlbsum.csv")
file_writer_lbsum = os.path.join(target_dir, "gndlbsum_master.csv")

# Append each csv file in the list to the output file
first_file = True
for file in merge_list:
    with open(file, "rU") as csv_file:
        reader = csv.reader(csv_file, delimiter="|")
        with open(file_writer_lbsum, "w" if first_file else "a") as f:
            writer = csv.writer(f, delimiter="|")
            # Unless it's the first file, skip its header row
            if not first_file:
                next(reader)
                first_file = False
            writer.writerows(reader)

【讨论】:

谢谢,就是这样。我不敢相信它是如此简单:) 是的,因为文件的行中没有任何默认的逗号分隔符,所以它将整个内容视为单个字符串,而 csv 编写器的默认设置是引用字符串.

以上是关于csv writer 在每一行周围加上引号的主要内容,如果未能解决你的问题,请参考以下文章

使用不带引号的python csv writer

如何将数据作为字符串(不是文件)写入 CSV 格式?

python csv writer添加了额外的引号

在字符串周围用引号编写csv(Python)

我啥时候应该在 shell 变量周围加上引号?

我啥时候应该在 shell 变量周围加上引号?