Python to excel..

This is the place for queries that don't fit in any of the other categories.

Python to excel..

Postby ChristopherL » Wed Sep 25, 2013 8:38 am

I have dictionary that I would like to print out to excel, any suggestions on how I can start up with this?
Or maybe if someone have a form or an old example that I can use..

Google only confuses me since it wants me to download different modules for assistance..

rgrds
Christopher
ChristopherL
 
Posts: 13
Joined: Tue Jul 16, 2013 2:03 pm

Re: Python to excel..

Postby Kebap » Wed Sep 25, 2013 9:10 am

I would recommend to do this by creating a CSV file. This is basically a mere text file with but some special format, which can then be read and interpreted by Excel, too. Then do something like this:

Code: Select all
import csv

my_dict = {"test": 1, "testing": 2}

with open('mycsvfile.csv', 'wb') as f:  # Just use 'w' mode in 3.x
    w = csv.DictWriter(f, my_dict.keys())
    w.writeheader()
    w.writerow(my_dict)


Which produces:

Code: Select all
test,testing
1,2
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 365
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: Python to excel..

Postby ChristopherL » Wed Sep 25, 2013 9:36 am

Kebap wrote:I would recommend to do this by creating a CSV file. This is basically a mere text file with but some special format, which can then be read and interpreted by Excel, too. Then do something like this:

Code: Select all
import csv

my_dict = {"test": 1, "testing": 2}

with open('mycsvfile.csv', 'wb') as f:  # Just use 'w' mode in 3.x
    w = csv.DictWriter(f, my_dict.keys())
    w.writeheader()
    w.writerow(my_dict)


Which produces:

Code: Select all
test,testing
1,2



For starters I am really bad at python, just jumped into it, i am more of a sql/access kinda guy, so sorry for being unprepared, i know it might annoy :)
Hmm okay this is quite new for me, so in my case, I have a dict called marginSEKDictSorted I want to use the dealDict[key] so for
Code: Select all
    for key, value in marginSEKDictSorted:
       
        row = dealDict[key]


In my case

Code: Select all
import csv

def PrintReportToExcel(MarginSEKDictSorted, DealDict):

marginSEKDictSorted = {TradeDate: row[TRADE_DATE], Dealer: row[Dlr_ID], MarginSEK: row[Deal_Margin]}

with open ('ExcelReport_CurrentDate.csv', 'wb') as f:
  w = csv.DictWriter(f,MarginSEKDictSorted.keys())
  w.writeheader()
  w.writerow(row)

ChristopherL
 
Posts: 13
Joined: Tue Jul 16, 2013 2:03 pm

Re: Python to excel..

Postby Kebap » Wed Sep 25, 2013 1:39 pm

Sorry I don't see your question. Also I don't understand your data structure. Do you have dicts inside dicts or something? Maybe provide a small executable example with some small set of realish data for us to review?
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 365
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 3 guests