2

Using Python v2.7.4:

I have the following CSV file:

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

I have been trying to change the file to:

Fulfillment,SKU,Qty
US,2000-000-300,3
US,2000-000-380,3
US,2000-000-400,3

So far I have the following code:

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]))

With this code I get the following output:

Fulfillment,SKU,Qty
2000-000-000-380,3
2000-000-000-400,3
2000-000-000-480,3

How do I insert US to the beginning column? (Just for reference there is more than just 3 rows in these csv files but for space I left out the rest.)

2 Answers 2

5

Just add a literal string to your row:

for row in r:
    w.writerow(('US', row[0], row[3]))
Sign up to request clarification or add additional context in comments.

Comments

2

If you're going to be doing a lot of csv manipulation, I strongly recommend looking at the pandas library. It makes a lot of things much simpler. Your code would become something like

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)

Some explanation follows. First, read in the csv file into an object called 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  

Then add a column called "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  

Then rename the axes:

>>> 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  

Select the columns you want:

>>> 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

And finally write it out to a csv, without including an extra index column (the numbers on the left, the row labels):

>>> 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

1 Comment

Thanks I'll definitely look into that library.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.