Python:将数据添加到第一个CSV列
使用Python v2.7.4: 我有以下CSV文件:Python:将数据添加到第一个CSV列,python,csv,Python,Csv,使用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 Orang
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
如何将我们插入到起始列?(仅供参考,这些csv文件中不止3行,但为了节省空间,我省略了其余部分。)只需在行中添加一个文字字符串:
for row in r:
w.writerow(('US', row[0], row[3]))
如果您要进行大量的
csv
操作,我强烈建议您查看库。它使很多事情变得简单得多。你的代码会变成
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文件读入名为
数据帧的对象中:
>>> 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
然后添加一个名为“履行”的列:
然后重命名轴:
>>> 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 = 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
>>> 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