python 以相同的顺序将字典保存到 csv
python save dictionary to csv in same order
这是我的实际代码:
import csv
fb_catalog_dict = {
"id":"",
"title":"",
"description":"",
"availability":"",
"condition":"",
"price":"",
"link":"",
"image_link":"",
"brand":"",
"additional_image_link":"",
"age_group":"",
"color":"",
"gender":"",
"item_group_id":"",
"google_product_category":"",
"material":"",
"pattern":"",
"product_type":"",
"sale_price":"",
"sale_price_effective_date":"",
"shipping":"",
"shipping_weight":"",
"size":"",
"custom_label_0":"",
"custom_label_1":"",
"custom_label_2":"",
"custom_label_3":"",
"custom_label_4":"",
}
with open('mycsvfile.csv', 'wb') as f: # Just use 'w' mode in 3.x
w = csv.DictWriter(f, fb_catalog_dict.keys())
w.writeheader()
w.writerow(fb_catalog_dict)
我想在 csv 中以与 fb_catalog_dict 相同的顺序使用相同的字典,问题是 python 使用不同的顺序字段创建我的 csv 文件,我该如何解决这个问题?
在 CPython >= 3.6 中,这会像写的那样工作,因为 .
在任何更早的 Python 版本(或不同的实现)上,您可以使用 collections.OrderedDict
。正如它的名字所暗示的那样。
您必须稍微更改实例化,因为将 dict
传递给 OrderedDict
只会保留 dict
的顺序,而不是您编写它的顺序。所以只需将其设为元组列表,as recommended here.
示例代码:
import csv
from collections import OrderedDict
fb_catalog_dict = OrderedDict([
("id", ""),
("title", ""),
("description", ""),
("availability", ""),
...
])
with open('mycsvfile.csv', 'wb') as f: # Just use 'w' mode in 3.x
w = csv.DictWriter(f, fb_catalog_dict.keys())
w.writerow(fb_catalog_dict)
(不确定您的 header
是什么定义,所以我将其省略。)
字典是无序的。并且鉴于您无论如何只使用键,您可以通过列表来实现您正在做的事情。即:
fb_catalog = [
"id",
"title",
"description",
]
等..
在Python中,字典是无序结构。字典中没有特定的顺序。所以我看到的唯一方法是使用字段列表来确保您始终以相同的顺序获取它们:
import csv
d = {'1':1,'2':2,'z':'z'}
def fb_catalog_dict(fields_list,dict_object):
return [dict_object[f] if f in dict_object else None for f in fields_list]
f = open('mycsvfile.csv', 'w',newline='')
writer = csv.writer(f)
for i in range(10):
writer.writerow(get_values_from_dict(['z','1','2'],d))
f.close()
P.S> 这个例子是 Python 3.x
这是我的实际代码:
import csv
fb_catalog_dict = {
"id":"",
"title":"",
"description":"",
"availability":"",
"condition":"",
"price":"",
"link":"",
"image_link":"",
"brand":"",
"additional_image_link":"",
"age_group":"",
"color":"",
"gender":"",
"item_group_id":"",
"google_product_category":"",
"material":"",
"pattern":"",
"product_type":"",
"sale_price":"",
"sale_price_effective_date":"",
"shipping":"",
"shipping_weight":"",
"size":"",
"custom_label_0":"",
"custom_label_1":"",
"custom_label_2":"",
"custom_label_3":"",
"custom_label_4":"",
}
with open('mycsvfile.csv', 'wb') as f: # Just use 'w' mode in 3.x
w = csv.DictWriter(f, fb_catalog_dict.keys())
w.writeheader()
w.writerow(fb_catalog_dict)
我想在 csv 中以与 fb_catalog_dict 相同的顺序使用相同的字典,问题是 python 使用不同的顺序字段创建我的 csv 文件,我该如何解决这个问题?
在 CPython >= 3.6 中,这会像写的那样工作,因为
在任何更早的 Python 版本(或不同的实现)上,您可以使用 collections.OrderedDict
。正如它的名字所暗示的那样。
您必须稍微更改实例化,因为将 dict
传递给 OrderedDict
只会保留 dict
的顺序,而不是您编写它的顺序。所以只需将其设为元组列表,as recommended here.
示例代码:
import csv
from collections import OrderedDict
fb_catalog_dict = OrderedDict([
("id", ""),
("title", ""),
("description", ""),
("availability", ""),
...
])
with open('mycsvfile.csv', 'wb') as f: # Just use 'w' mode in 3.x
w = csv.DictWriter(f, fb_catalog_dict.keys())
w.writerow(fb_catalog_dict)
(不确定您的 header
是什么定义,所以我将其省略。)
字典是无序的。并且鉴于您无论如何只使用键,您可以通过列表来实现您正在做的事情。即:
fb_catalog = [
"id",
"title",
"description",
]
等..
在Python中,字典是无序结构。字典中没有特定的顺序。所以我看到的唯一方法是使用字段列表来确保您始终以相同的顺序获取它们:
import csv
d = {'1':1,'2':2,'z':'z'}
def fb_catalog_dict(fields_list,dict_object):
return [dict_object[f] if f in dict_object else None for f in fields_list]
f = open('mycsvfile.csv', 'w',newline='')
writer = csv.writer(f)
for i in range(10):
writer.writerow(get_values_from_dict(['z','1','2'],d))
f.close()
P.S> 这个例子是 Python 3.x