gpt4 book ai didi

python - 将 namedtuple 转换为 python 列表并生成 CSV

转载 作者:太空宇宙 更新时间:2023-11-03 13:17:24 24 4
gpt4 key购买 nike

引用:https://stackoverflow.com/a/19726081/1182021

OrderedDict([
('company 1', tup(price=246, year='1991', month='march')),
('company 2', tup(price=245, year='1990', month='jan')),
('company 3', tup(price=243, year='1990', month='jan')),
('company 4', tup(price=247, year='1991', month='december')),
('company 5', tup(price=245, year='1991', month='june'))])

如何以这样的 csv 格式导出此数据:

Company Name , Price , Year , Month
Company 1 , 246 , 1991 , march
Company 2 , 245 , 1990 , jan

我尝试像这样使用 import csv 创建一个 csv(ALL IN SAME ROW):

myfile = open(csvfile, 'wb')
wr = csv.writer(myfile , quoting=csv.QUOTE_ALL)
wr.writerow(data_list)

编辑 1

data_list 是 OrderedDict,我得到的所有数据格式都不正确

Company-A | Filtered(Year='2013', Month='Dec', Price=0) Company-B   | Filtered(Year='2013', Month='Dec', Price=0) |     Company-C  |    Filtered(Year='2013', Month='Dec', Price=0) |   Company-D |     Filtered(Year='2013', Month='Dec', Price=0) Company-E |     Filtered(Year='2013', Month='Dec', Price=0)

编辑 2

好的,我通过 data_list = OrderedDict.items()

将 OrderedDict 转换为简单列表

并按照 mkrehili 的建议我将此作为我的导出方法:

def ExportData(csv_file, data_list):
csv_file = open(csv_file, 'wb')
wr = csv.writer(csv_file, quoting=csv.QUOTE_ALL)

for company_name, company_data in data_list:
wr.writerow([company_name] + list(company_data))

我现在得到这个列表:

[('Company-A', Filtered(Year='2013', Month='Dec', Price=0)), ('Company-B', Filtered(Year='2013', Month='Dec', Price=0)), ('Company-C', Filtered(Year='2013', Month='Dec', Price=0)), ('Company-D', Filtered(Year='2013', Month='Dec', Price=0)), ('Company-E', Filtered(Year='2013', Month='Dec', Price=0))]

但是当我将其转换为 CSV 时,输出如下:

Company-A   2013    Dec 0
Company-B 2013 Dec 0
Company-C 2013 Dec 0
Company-D 2013 Dec 0
Company-E 2013 Dec 0

编辑 3实际上这两个数据并不相同:

在 OrderedDict 中:

OrderedDict([
('company A', tup(price=246, year='1991', month='march')),
('company B', tup(price=245, year='1990', month='jan')),
('company C', tup(price=243, year='1990', month='jan')),
('company D', tup(price=247, year='1991', month='december')),
('company E', tup(price=245, year='1991', month='june'))])

但是一旦我执行 data_list = OrderedDict.items() 它就会给我这个数据: 格式不正确:

[('Company-A', tup(price=0, year="2013", month='Dec')),
('Company-B', tup(price=0, year="2013", month='Dec')),
('Company-C', tup(price=0, year="2013", month='Dec')),
('Company-D', tup(price=0, year="2013", month='Dec')),
('Company-E', tup(price=0, year="2013", month='Dec'))]

所以主要我的问题是创建一个简单的列表,我有这样的列表:

Company Name , Price , Year , Month
Company A , 246 , 1991 , march
Company B , 245 , 1990 , jan
......
......

编辑 4

with open(csv_file, 'w') as f:
w = csv.writer(f)
w.writerow(('Company Name', 'Year', 'Month', 'Price'))
w.writerows([(name, data.year, data.month, data.price) for name, data in data_list])`

这给了我正确的导出,但在每一行之后我都有一个空行,如下所示:

Company Name    Year    Month   Price

Company-A 2000 Mar 1000

Company-B 2007 Mar 986

Company-C 1993 Jun 995

Company-D 2002 Apr 999

Company-E 2008 Oct 997

最佳答案

import csv
from collections import namedtuple, OrderedDict

tup = namedtuple('tup', ['price', 'year', 'month'])
prices = OrderedDict([
('company A', tup(price=246, year='1991', month='march')),
('company B', tup(price=245, year='1990', month='jan')),
('company C', tup(price=243, year='1990', month='jan')),
('company D', tup(price=247, year='1991', month='december')),
('company E', tup(price=245, year='1991', month='june'))])

with open('output.csv', 'w') as f:
w = csv.writer(f)
w.writerow(('Company Name', 'Price', 'Year', 'Month')) # field header
w.writerows([(name, data.price, data.year, data.month) for name, data in prices.items()])

写入 output.csv:

Company Name,Price,Year,Month
company A,246,1991,march
company B,245,1990,jan
company C,243,1990,jan
company D,247,1991,december
company E,245,1991,june

关于python - 将 namedtuple 转换为 python 列表并生成 CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24839649/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com