[英]Get help for Python code that get data from one CSV file and insert it into another CSV file
我正在进行数据迁移。旧应用程序数据导出为一个 CSV 文件。 我们无法将此 CSV 文件直接导入新应用程序。 需要创建与新应用程序匹配的新 CSV 模板,并将一些数据导入这个新的 CSV 模板。 我想请求满足此要求的代码。
我不确定你想要 go 到什么模板。 我将假设您想要更改列的数量/顺序或分隔符。
最简单的就是一行一行的读,然后写:
import csv
with open("Old.csv", 'r') as readfp, open("new.csv", 'w') as writefp:
csvReader = csv.reader(readfp)
csvWriter = csv.writer(writefp, delimiter=',')
for line in csvReader:
#line is a list of strings so you can reorder it as you wish. I'll skip the third column as an example.
csvWriter.writerow(line[:2]+line[3:])
如果你安装了 pandas 这更简单
import pandas as pd
df = pd.read_csv("Old.csv")
df.drop(labels=["name_of_bad_col1", "name_of_bad_col2"], sep=',')
df.to_csv("new.csv)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.