How to write a list of tuple in python with header mapping -


i have process python list of tuples each tuple contains header name below- want tuple mapped respectives header in tuple.

    [[(u'index', u' broad market indices :')],     [(u'index', u'cnx nifty'), (u'current', u'7,950.90'), (u'% change', u'0.03'), (u'open', u'7,992.05'), (u'high', u'8,008.25'), (u'low', u'7,930.65'), (u'prev. close', u'7,948.90'), (u'today', u''), (u'52w high', u'9,119.20'), (u'52w low', u'7,539.50')],     [(u'index', u'cnx nifty junior'), (u'current', u'19,752.40'), (u'% change', u'0.73'), (u'open', u'19,765.10'), (u'high', u'19,808.25'), (u'low', u'19,629.50'), (u'prev. close', u'19,609.75'), (u'today', u''), (u'52w high', u'21,730.80'), (u'52w low', u'16,271.45')]] 

now want write list csv looks in ms excel below-

csv

thanks in advance.

i found workaround @ last-

convert nested lists dictionary , use dictwriter-

import csv my_d = [] in datalist:     my_d.append({x:y x,y in i})  open("file.csv",'wb') f:    # using dictionary keys fieldnames csv file header    writer = csv.dictwriter(f, my_d[1].keys())    writer.writeheader()    d in my_d:       writer.writerow(d) 

but changes order of columns. if previous order needed used header declaration , used in dictwrites.

headers = [u'index', u'current', u'% change', u'open', u'high', u'low', u'prev. close', u'today', u'52w high', u'52w low'] 

and used below

   writer = csv.dictwriter(f, headers) 

that's solved problem..


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -