分隔Python脚本

时间:2017-09-28 17:52:24

标签: python csv

我从Rally服务器提取数据,并希望将其写入CSV文件并创建快照。 我正在创建文件:

orig_stdout = sys.stdout
timestr = time.strftime("%m-%d-%Y")
f = open(timestr+'FileName.csv', 'w')
sys.stdout = f

#all of the setup and functions

sys.stdout = orig_stdout
f.close()

我写给文件的输出来自:

for item in response:
    print("%s, %s, %s, %s" % (item.Attribute1, item.Attribute2, item.Attribute3, item.Attribute4))

问题是字符串偶尔会有逗号。每个平常的角色都是如此。我不想替换所述字符串中的逗号。

我是否可以使用Excel CSV文件识别并能够正确拆分成列的特殊字符,或者我能以某种方式告诉文件使用多个字符分隔,例如" ,,&# 34;

1 个答案:

答案 0 :(得分:1)

Python有一个内置的csv模块,可以处理各种格式(例如excel)  并且可以自动处理特殊选项(例如转义)。

Writer object from the CSV module听起来像你正在寻找的。