简体   繁体   English

将数据附加到 csv 文件中的行尾(Python)

[英]Append Data to the end of a row in a csv file (Python)

I am attempting to append 4 elements to the end of a row in a csv file我正在尝试将 4 个元素附加到 csv 文件中的行尾

Original
toetag1,tire11,tire12,tire13,tire14

Desired Outcome
toetag1,tire11,tire12,tire13,tire14,wtire1,wtire2,wtire3,wtire4

I attempted to research ways to do this how ever most search results yielded results such as "how to append to a csv file in python"我试图研究如何做到这一点的大多数搜索结果如何产生结果,例如“如何附加到 python 中的 csv 文件”

Can someone direct me in the correct way to solve this problem有人可以指导我以正确的方式解决这个问题吗

I advise you to use pandas module and read_csv method.我建议您使用 pandas 模块和 read_csv 方法。

You can use the following code for instance :例如,您可以使用以下代码:

data = pandas.read_csv("your_file.csv")
row = data.iloc[0].to_numpy()
row.append(['wtire1','wtire2','wtire3','wtire4'])

You can read the csv file to a list of lists and do the necessary manipulation before writing it back.您可以将 csv 文件读取到列表列表中,并在写回之前进行必要的操作。

import csv

#read csv file
with open("original.csv") as f:
    reader = csv.reader(f)
    data = [row for row in reader]

#modify data
data[0] = data[0] + ["wtire1", "wtire2", "wtire3", "wtire4"]

#write to new file
with open("output.csv", "w") as f:
    writer = csv.writer(f)
    writer.writerows(data)

暂无
暂无

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

相关问题 Python CSV,如何在一行一行(一行一行)读取数据的同时在行尾追加数据? - Python CSV, How to append data at the end of a row whilst reading it line by line (row by row)? 使用Python在CSV文件的特定行中附加/更新数据 - Append/Update data in a specific row in a CSV file using Python 将数据附加到python中的文件末尾 - Append data to the end of a file in python 如何将 append 列为 csv 文件中的行 python? - How to append a list as row in csv file in python? 使用python在csv中每一行的末尾附加字典或值列表 - Append Dictionary or list of values at the end of each row in csv using python 获取 CSV 文件的最后一行并将其附加到同一个 CSV 文件的末尾 - Get last row of CSV file and append it onto the end of the same CSV file 在python中将数据附加到.csv文件时删除空行,每添加一次该行跳1个空行 - Remove empty rows when append data to .csv file in python, the row jump 1 empty row every i add the data Python-有时会将CSV文件的附加行添加到前一行的末尾 - Python - Sometimes an appended row to a CSV file is added to the end of the previous row 如何将数据添加到CSV文件的末尾(Python) - How to add data to the end of a CSV file (Python) 指定开始和结束行号后,从Python的dataframe / csv文件中选择随机行(数据)? - Selecting random rows (of data) from dataframe / csv file in Python after designating start and end row number?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM