如何在python中将新列附加到csv中?

时间:2015-11-04 12:51:19

标签: python csv

[INPUT]

listBox = ['status: 123', 'company: test']

[期望的输出]

'zzzz' - column A already exists
'status: 123' - should be written in column B in a single cell.  
'company: test' - should be written in column C in a single cell.

我试过了:

编辑1

def writeToCSV():
    checkpointList = ['CompanyID: test', 'StatusCode: 123']
    data = [str(checkpointList).split(",")]
    path = "output.csv"
    with open(path, "wb") as csv_file:
        writer = csv.writer(csv_file, delimiter=',')
        for line in data:
            writer.writerow(line)
writeToCSV()

但这会覆盖现有的列

1 个答案:

答案 0 :(得分:0)

请尝试以下操作 - writerow预计单list

import csv
def writeToCSV():
    checkpointList = ['CompanyID: test', 'StatusCode: 123']
    path = r"C:\out.txt"
    with open(path, "wb") as csv_file:
        writer = csv.writer(csv_file, delimiter=',')
        writer.writerow(checkpointList)
writeToCSV()

它写道 -

CompanyID: test,StatusCode: 123