使用 Python v2.7.4:
我有以下 CSV 文件:
Item Number,Item Description,List Price,QTY Available
2000-000-000-380,AC - CF/M Series Green For White Hood,299.99,3
2000-000-000-400,AC - CF/M Series Orange For Black Hood,299.99,3
2000-000-000-480,AC - CF/M Series Orange For White Hood,299.99,3
我一直在尝试将文件更改为:
Fulfillment,SKU,Qty
US,2000-000-300,3
US,2000-000-380,3
US,2000-000-400,3
到目前为止,我有以下代码:
import csv
import os
inputFileName = "temp_modified.csv"
outputFileName = os.path.splitext(inputFileName)[0] + "_pro.csv"
with open(inputFileName, "rb") as inFile, open(outputFileName, "wb") as outfile:
r = csv.reader(inFile)
w = csv.writer(outfile)
r.next()
w.writerow(['Fulfillment', 'SKU', 'Qty'])
for row in r:
w.writerow((row[0], row[3]))
使用此代码,我得到以下输出:
Fulfillment,SKU,Qty
2000-000-000-380,3
2000-000-000-400,3
2000-000-000-480,3
如何将 US 插入到开始列?(仅供参考,这些 csv 文件中不仅有 3 行,但为了空间,我省略了其余的。)