[Tutor] Re: you may regret that comment Magnus!!

Magnus Lycka magnus@thinkware.se
Mon Feb 3 03:42:02 2003


At 19:31 2003-02-02 -0800, Mic Forster wrote:
>the output is printed as a list in a single line.

I'm reverting this back to the list--tell me if you
want a paid private consultation instead...  ;)

To get two lists printed in a column each, you can do:

 >>> for a_b_pair in zip(a,b):
...     print "%s\t%s" % a_b_pair
...
1       a
2       b
3       c
4       d
5       e

>Ideally
>I would like to copy and paste the expected results
>into Excel as a column rather than a row.

You can let Python do that! (Assumes the win32all package.
(It's included by default in ActivePython, or can be
installed separately.)

 >>> import win32com.client
 >>> xl = win32com.client.Dispatch("Excel.Application")
 >>> xl.visible = 1
 >>> xl.Workbooks.Add()
<COMObject Add>
 >>> row = 1
 >>> col = 1
 >>> for value in a:
...     xl.Cells(row, col).Value = value
...     row += 1
...
 >>> row = 1
 >>> col = 2
 >>> for value in b:
...     xl.Cells(row, col).Value = value
...     row += 1

Unfortunately, these things are not so easy to find in the
docs. The win32all docs give generic information, but you
have to go to Excel to find Visual Basic functions, objects,
methods etc,and then translate how to translate that to
Python.

Another option is to simply put your data in the clipboard,
and just go to excel and paste where you like.

 >>> data = "\n".join(["%s\t%s" % x for x in zip(a,b)])
 >>> print data
1       a
2       b
3       c
4       d
5       e
 >>> # Looks ok.
 >>> import win32clipboard
 >>> win32clipboard.OpenClipboard()
 >>> win32clipboard.EmptyClipboard()
 >>> win32clipboard.SetClipboardText(data)
8782532
 >>> win32clipboard.CloseClipboard()

Now, just mark a cell in Excel and do Ctrl-V.


-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se