简体   繁体   English

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

[英]How can I append new columns into csv in python?

[INPUT] [输入]

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

[DESIRED OUTPUT] [期望的输出]

'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.

I've tried: 我试过了:

Edit 1 编辑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()

But this will overwrite the existing columns 但这将覆盖现有的列

Try below- writerow expects single list . 尝试低于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()

It writes- 它写道:

CompanyID: test,StatusCode: 123

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python + CSV:如何在Python中创建一个新的csv并为此写入新的列? - Python + CSV : How can I create a new csv and write new columns to that in Python? 如何将 csv 中的 append 列与 python - how to append columns in csv with python 如何将 append 的数据列表添加到 CSV 中的多个新列? - How do I append a list of data to multiple new columns in a CSV? 如何获取 csv 列中的出现次数并保存为包含 python 中的计数的新 csv - How can I get a count of occurrances in csv columns and save as a new csv containing the count in python 从python中的其他csv列追加到新的csv - Append to a new csv from other csv columns in python 如何从带有图像注释的 CSV 文件中提取值并将它们附加到 Python 中 RetinaNet 的新 CSV 文件中? - How can I extract values from a CSV file with image annotations and append them to a new CSV file for RetinaNet in Python? 如何在python中将新行附加到csv文件 - How to append a new line to a csv file in python 如何在 Python 的 CSV 文件中 append 一个新值? - How to append a new value in a CSV file in Python? 如何将 CSV 文件附加到 Python 中的现有 zip 存档? - How can I append a CSV file to an existing zip archive in Python? 如何在python中将整列追加到表(列表列表)? - How can I append whole columns to a table (list of lists) in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM