简体   繁体   中英

converting single column csv into multiple column csv using python?

the code I have given below will convert and API response into a CSV file, but its dumping entire response of the API into once column, but I need in different columns with the header of 'SKU', 'Category_name', 'price', 'specialPrice', 'sizes', 'offers'

the data inside is object, object, float64, float64, object(this is the sizes we have to mention, the thing is its a list), object, and entire thing is in list

import requests
import json
import time
import datetime
from datetime import datetime

my_data = {
"category_ids" : "948",
"limit" : "10000"
}

my_headers = {
  'Content-Type': 'application/json'
}

response = requests.post('https://newapi.zivame.com/api/v1/catalog/list', data=json.dumps(my_data),
                     headers=my_headers)


data = response.json()
products = data.get("data").get("docs")
x=products.get(0)
dataList = []



for product in products:
    valuepack = product.get("valuePackOffers",None)
    offer = []
    if valuepack:
        for v in valuepack:
            offer.append(v.get("display_name",None))
    #ts = datetime.now().timestamp()
    #datatime_data = datetime.fromtimestamp(ts)


    dataList.append([product.get("sku",None), product.get("mainCategoryName",None), product["price"], product["specialPrice"], product.get("sizes",None), offer])

len(dataList)




with open(r'/home/arcod/Downloads/data.json','w') as js:
    json.dump(dataList, js)


#import pandas
#from pandas.io.json import json_normalize
#data1 = json.load('/home/arcod/Downloads/data.json')
#norm_data = pd.DataFrame(json_normalize(data1))
#import os
#os.open(r'/home/arcod/Downloads/test.csv','w')
#norm_data.to_csv('/home/arcod/Downloads/test.csv')

import csv

with open(r'/home/arcod/Downloads/promos_zivame.csv','w') as csvFile:
    writer = csv.writer(csvFile)
    writer.writerows(dataList)


import pandas as pd

dl = pd.read_csv('/home/arcod/Downloads/promos_zivame.csv','wb', delimiter=",", error_bad_lines=False)
dl.dtypes
print(dl.dtypes)
new_dl = pd.DataFrame()

index = 1
for i in range(0, len(dl)):
    new_dl['' + str(index)] = dl[0].iloc[i:i+10].reset_index(drop=True)
    index +=1

print(new_dl)



print('Done')

changes in your csv writing function

import csv

with open(r'/home/arcod/Downloads/promos_zivame.csv','w') as csvFile:
    writer = csv.writer(csvFile)
    writer.writerow(('SKU', 'Category_name', 'price', 'specialPrice', 'sizes', 'offers'))
    writer.writerows(dataList)

instead of saving dataList as json , why can't you save it as a csv file.

import pandas as pd
dl = pd.DataFrame(dataList, columns = ['SKU', 'Category_name', 'price', 'specialPrice', 'sizes', 'offers'])
dl.to_csv('/home/arcod/Downloads/promos_zivame.csv')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM