Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert namedtuple to list in python and generate CSV

Ref: 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'))])

How to export this data in csv format like this:

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

I tried creating a csv using import csv like this (ALL IN SAME ROW):

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

EDIT 1

where data_list is the OrderedDict and I am getting all data incorrect format

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)

EDIT 2

OK I converted OrderedDict to simple list by data_list = OrderedDict.items()

And as suggested by mkrehili I have this as my export method:

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

And I am getting this list now:

[('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))]

But when I am converting it in CSV the following is the output:

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

EDIT 3 Actually both the data are not coming same:

In OrderedDict its:

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

But once I do data_list = OrderedDict.items() it gives me this data: Which is not in correct format:

[('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'))]

So majorly my issue is to create a simple list where i have list like this:

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

EDIT 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])`

This is giving me right export, but after each line I am having an empty row, like this:

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
like image 220
Django Anonymous Avatar asked Jul 19 '14 11:07

Django Anonymous


2 Answers

namedtuple is still iterable so treat it like such:

>>> tup = namedtuple('tup', ['price', 'year', 'month'])
>>> c = tup(price=246, year='1991', month='march')
>>> list(c)
[246, '1991', 'march']
>>> print(*c)
246 1991 march

So in your example (you didn't post complete code, which makes things harder):

for company_name, company_data in data_list.items():
    wr.writerow([company_name] + list(company_data))
like image 41
mkriheli Avatar answered Oct 06 '22 00:10

mkriheli


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()])

Writes to 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
like image 98
mhawke Avatar answered Oct 05 '22 22:10

mhawke