Python将字典转换为CSV [英] Python convert dictionary to CSV

查看:2041
本文介绍了Python将字典转换为CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将字典转换为CSV,以便可读(在其各自的键中).

import csv
import json
from urllib.request import urlopen
x =0
id_num = [848649491, 883560475, 431495539, 883481767, 851341658, 42842466, 173114302, 900616370, 1042383097, 859872672]
for bilangan in id_num:

with urlopen("https://shopee.com.my/api/v2/item/get?itemid="+str(bilangan)+"&shopid=1883827")as response:
    source = response.read()

data = json.loads(source)
#print(json.dumps(data, indent=2))


data_list ={ x:{'title':productName(),'price':price(),'description':description(),'preorder':checkPreorder(),
             'estimate delivery':estimateDelivery(),'variation': variation(), 'category':categories(),
             'brand':brand(),'image':image_link()}}

#print(data_list[x])
x =+ 1

我将数据存储在x中,因此它将从0循环到1、2等.我已经尝试了很多方法,但是仍然找不到找到使其看起来像这样或接近这个样子的方法:

https://i.stack.imgur.com/WoOpe.jpg

解决方案

使用csv模块中的DictWriter

演示:

import csv

data_list ={'x':{'title':'productName()','price':'price()','description':'description()','preorder':'checkPreorder()',
             'estimate delivery':'estimateDelivery()','variation': 'variation()', 'category':'categories()',
             'brand':'brand()','image':'image_link()'}}

with open(filename, "w") as infile:
    writer = csv.DictWriter(infile, fieldnames=data_list["x"].keys())
    writer.writeheader()
    writer.writerow(data_list["x"])

I am trying to convert dictionary to CSV so that it is readable (in their respective key).

import csv
import json
from urllib.request import urlopen
x =0
id_num = [848649491, 883560475, 431495539, 883481767, 851341658, 42842466, 173114302, 900616370, 1042383097, 859872672]
for bilangan in id_num:

with urlopen("https://shopee.com.my/api/v2/item/get?itemid="+str(bilangan)+"&shopid=1883827")as response:
    source = response.read()

data = json.loads(source)
#print(json.dumps(data, indent=2))


data_list ={ x:{'title':productName(),'price':price(),'description':description(),'preorder':checkPreorder(),
             'estimate delivery':estimateDelivery(),'variation': variation(), 'category':categories(),
             'brand':brand(),'image':image_link()}}

#print(data_list[x])
x =+ 1

i store the data in x, so it will be looping from 0 to 1, 2 and etc. i have tried many things but still cannot find a way to make it look like this or close to this:

https://i.stack.imgur.com/WoOpe.jpg

解决方案

Using DictWriter from the csv module

Demo:

import csv

data_list ={'x':{'title':'productName()','price':'price()','description':'description()','preorder':'checkPreorder()',
             'estimate delivery':'estimateDelivery()','variation': 'variation()', 'category':'categories()',
             'brand':'brand()','image':'image_link()'}}

with open(filename, "w") as infile:
    writer = csv.DictWriter(infile, fieldnames=data_list["x"].keys())
    writer.writeheader()
    writer.writerow(data_list["x"])

这篇关于Python将字典转换为CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆