简体   繁体   English

使用python将单列csv转换为多列csv?

[英]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'我在下面给出的代码会将 API 响应转换为 CSV 文件,但它将 API 的整个响应转储到一个列中,但我需要在不同的列中使用“SKU”、“Category_name”、“price”标题, “特价”、“尺寸”、“优惠”

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里面的数据是object,object,float64,float64,object(这是我们不得不提到的大小,事物是它的列表),对象,整个事物都在列表中

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你的 csv 书写功能的变化

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.而不是将dataList保存为 json ,为什么不能将其保存为csv文件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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