2

使用 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 行,但为了空间,我省略了其余的。)

4

2 回答 2

5

只需在您的行中添加一个文字字符串:

for row in r:
    w.writerow(('US', row[0], row[3]))
于 2013-05-01T15:20:59.903 回答
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 文件读入一个名为 a 的对象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
于 2013-05-01T15:52:07.907 回答