[英]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.