使用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行,但是对于空间我遗漏了其余部分。)
答案 0 :(得分:5)
只需在您的行中添加文字字符串:
for row in r:
w.writerow(('US', row[0], row[3]))
答案 1 :(得分:2)
如果您要进行大量csv
操作,我强烈建议您查看pandas库。它使很多事情变得更加简单。您的代码将变为类似
import pandas as pd
df = pd.read_csv("temp_modified.csv")
df["Fulfillment"] = "US"
df = df.rename_axis({"Item Number": "SKU", "QTY Available": "QTY"})
df = df[["Fulfillment", "SKU", "QTY"]]
df.to_csv("temp_modified_pro.csv", index=False)
以下是一些解释。首先,将csv文件读入名为DataFrame
的对象:
>>> import pandas as pd
>>> df = pd.read_csv("temp_modified.csv")
>>> df
Item Number Item Description List Price \
0 2000-000-000-380 AC - CF/M Series Green For White Hood 299.99
1 2000-000-000-400 AC - CF/M Series Orange For Black Hood 299.99
2 2000-000-000-480 AC - CF/M Series Orange For White Hood 299.99
QTY Available
0 3
1 3
2 3
然后添加一个名为“Fulfillment”的列:
>>> df["Fulfillment"] = "US"
>>> df
Item Number Item Description List Price \
0 2000-000-000-380 AC - CF/M Series Green For White Hood 299.99
1 2000-000-000-400 AC - CF/M Series Orange For Black Hood 299.99
2 2000-000-000-480 AC - CF/M Series Orange For White Hood 299.99
QTY Available Fulfillment
0 3 US
1 3 US
2 3 US
然后重命名轴:
>>> df = df.rename_axis({"Item Number": "SKU", "QTY Available": "QTY"})
>>> df
SKU Item Description List Price QTY \
0 2000-000-000-380 AC - CF/M Series Green For White Hood 299.99 3
1 2000-000-000-400 AC - CF/M Series Orange For Black Hood 299.99 3
2 2000-000-000-480 AC - CF/M Series Orange For White Hood 299.99 3
Fulfillment
0 US
1 US
2 US
选择所需的列:
>>> df = df[["Fulfillment", "SKU", "QTY"]]
>>> df
Fulfillment SKU QTY
0 US 2000-000-000-380 3
1 US 2000-000-000-400 3
2 US 2000-000-000-480 3
最后写出来给csv,不包括额外的索引列(左边的数字,行标签):
>>> df.to_csv("temp_modified_pro.csv", index=False)
>>> !cat temp_modified_pro.csv
Fulfillment,SKU,QTY
US,2000-000-000-380,3
US,2000-000-000-400,3
US,2000-000-000-480,3