简体   繁体   中英

Dictionary to CSV file: Ordering of columns

I am trying to export a list of dictionaries to a .csv file:

keys = hist[0].keys()
with open(file, 'wt') as output_file:
    dict_writer = csv.DictWriter(output_file, keys, lineterminator='\n')
    dict_writer.writeheader()
    dict_writer.writerows(hist)

I want the last key in the dictionary to be the first key in the column.

My list of dictionaries (hist) is:

[{'RSD': '-', 'GBP': '0.500409', 'Date': '2008-04-05'}, 
 {'RSD': '-', 'GBP': '0.500409', 'Date': '2008-04-06'}, 
 {'RSD': '-', 'GBP': '0.50331', 'Date': '2008-04-07'}, 
 {'RSD': '-', 'GBP': '0.507939', 'Date': '2008-04-08'},
...

Which I got by:

for x in dates:
    l = {}
    for y in currs:
        try:
            m = exrates.get_exrates(x)[y]
        except KeyError:
            m = '-'
        l[y] = m
    l['Date'] = x
    hist.append(l)

How do I either change the ordering of the dictionary or change the ordering of the columns?

Hard code the header with the order you want.

keys = ['Date', 'RSD', 'GBP']
with open(file, 'wt') as output_file:
    dict_writer = csv.DictWriter(output_file, keys, lineterminator='\n')
    dict_writer.writeheader()
    dict_writer.writerows(hist)

hard coding is preferred any time you want your csv to have a specific format.

Use an OrderedDict if you want order and reverse:

from collections import OrderedDict

for x in dates:
    l = OrderedDict()
    for y in currs:
        try:
            m = exrates.get_exrates(x)[y]
        except KeyError:
            m = '-'
        l[y] = m
    l['Date'] = x
    hist.append(l)

To get the reverse order use reversed:

keys = list(reversed(list(hist[0].keys())))
print(keys)

If you just want to have the last key atthe front:

k = list(hist[0].keys())

keys = keys[-1] +  key[:-1]
print(keys)

You can also use a comprehension using dict.get :

for x in dates:
    l = OrderedDict((y, exrates.get_exrates(x).get(y, "-")) for y in currs)
    l['Date'] = x
    hist.append(l)

If you are going to supply the headers and don't want ordered dicts after just write as you go, presuming curr is a list you can use curr as the header adding Date:

import csv

with open(file, 'wt') as output_file:
    wr = csv.writer(output_file)
    wr.writerow(["Date"] + currs)
    for x in dates:
        row = [x] + [exrates.get_exrates(x).get(y, "-") for y in currs]
        wr.writerow(row)

The elements in curr are what you are using as the keys which are your headers so storing dicts is not needed if all you want to do is write the contents of [exrates.get_exrates(x).get(y, "-") for y in currs] with x written to the first column under Date.

(If you're not averse to using pandas ) You could do this by simply re-ordering the columns, look at the last step:

In [1]: import pandas as pd

In [2]: x = [{'RSD': '-', 'GBP': '0.500409', 'Date': '2008-04-05'}, 
   ...:  {'RSD': '-', 'GBP': '0.500409', 'Date': '2008-04-06'}, 
   ...:  {'RSD': '-', 'GBP': '0.50331', 'Date': '2008-04-07'}, 
   ...:  {'RSD': '-', 'GBP': '0.507939', 'Date': '2008-04-08'},]

In [3]: pd.DataFrame(x)
Out[3]: 
         Date       GBP RSD
0  2008-04-05  0.500409   -
1  2008-04-06  0.500409   -
2  2008-04-07   0.50331   -
3  2008-04-08  0.507939   -

In [4]: y = pd.DataFrame(x)

In [5]: y = y[['RSD', 'GBP', 'Date']]

In [6]: y
Out[6]: 
  RSD       GBP        Date
0   -  0.500409  2008-04-05
1   -  0.500409  2008-04-06
2   -   0.50331  2008-04-07
3   -  0.507939  2008-04-08

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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