【发布时间】:2012-02-22 05:34:44
【问题描述】:
我想将输入 .csv 文件中的组合字段组合起来以输出到 .csv 文件,其中一些包含逗号。这是我的简化代码
outfile = open('output.csv', 'w')
#these values are made up for this example; normally they would be read from
#a csv and passed to the following 'combine()' function
a = "John"
b = ",Jr."
def combine(a, b):
if a == "":
pass #don't write anything if the field is empty
else:
outfile.write(a)
if b =="":
pass
else:
outfile.write(b)
如果 b 以逗号开头,如何输出“John, Jr.” ?我曾尝试使用 csv.writer writerow() 但它在每个字符之间放置了一个逗号分隔符。我尝试定义一个escapechar,但它只输出“John \”、“Jr”。有什么建议吗?
【问题讨论】: