【发布时间】:2015-12-06 02:08:22
【问题描述】:
如何在 python 中创建一个字符串列表,其中字符串可以包含逗号,这样解释器就不会过早地中断逗号。
我正在阅读由 MSExcel 创建的模板 csv 文件。内容为 2 列:col1 = 颜色,col2 = 可以包含逗号的句子。内容如下:
Light Green,"Matches type, instance, etc."
Red,Happens in BD10 but not BD20
Blue,"Instance and time matches, type doesn't match"
Yellow,Caught on replay
当我读入并输出所读内容时,它会在所有逗号上打断输入行。
#my code snippet, please excuse any typos during sanitization
with open('outfile.csv','r') as infile:
blah = infile.readlines()
for i in blah:
line = i.strip().split(",")
print line
My output:
['Light Green', '"Matches type', ' instance', ' etc."']
['Red', 'Happens in BD10 but not BD20']
['Blue', '"Instance and time matches', ' type doesn\'t match"']
['Yellow', 'Caught on replay']
我如何告诉 python 在 "" 内忽略逗号并在所有其他时间以逗号分隔?
【问题讨论】: