Via Matt Harrison's blog post, here is how to write Excel or OpenOffice.org Calc spreadsheet files using Python and the xlwt library. Xlwt is a fork of pyExcelerator which handles only writing spreadsheet files. For reading spreadsheets, see xlrd. Note, these libraries don't use COM, so they will work on non-Windows OSes, such as Linux. For more information, see Matt's blog post. He even has a PDF cheat sheet.
sudo pip install -U xlwt
import xlwt
DATA = (("The Essential Calvin and Hobbes", 1988,),
("The Authoritative Calvin and Hobbes ...I always forget how to use Python's setdefault dictionary operation so here is a quick example.
What I want:
DATA_SOURCE = (('key1', 'value1'),
('key1', 'value2'),
('key2', 'value3'),
('key2', 'value4'),
('key2', 'value5'),)
newdata = {}
for k, v in DATA_SOURCE:
if newdata.has_key(k):
newdata[k].append(v)
else:
newdata[k] = [v]
print newdata
Results:
{'key2': ['value3', 'value4', 'value5'], 'key1': ['value1', 'value2']}
Better way using setdefault:
newdata = {}
for k, v in DATA_SOURCE:
newdata.setdefault(k, []).append(v)
print newdata
The results are the same.
... read more »I sometimes need to copy a file (such as a database dump) between two remote hosts on EC2. Normally this involves a few steps: scp'ing the ssh keyfile to Host 1, ssh'ing to Host 1, looking up the address for Host 2, then scp'ing the desired file from Host 1 to Host 2.
I was excited to read in the man page that scp can copy files between two
remote hosts directly. However, it didn't work for me.
Apparently,
running scp host1:myfile host2: is like running
ssh host1 scp myfile host2: so I still need ...
I needed to parse through my Nginx log files to debug a problem. However, the logs are separated into many files, most of them are gzipped, and I wanted the ordering within the files reversed. So I abstracted the logic to handle this into a function. Now I can pass a glob pattern such as /var/log/nginx/cache.log* to my function, and iterate over each line in all the files as if they were one file. Here is my function. Let me know if there is a better way to do this.
Update 2010-02-24:To handle multiple log ...
... read more »Even though the pstats module (used by cProfile) is part of the Python Standard Library, Ubuntu requires installing a separate package because of its non-free license. For more information, see this Ubuntu bug report. (thanks Luke)
Running this on Ubuntu Karmic:
import cProfile
def my_super_slow_routine(): pass
cProfile.run('my_super_slow_routine()')
Produces this error:
Traceback (most recent call last): File "test_pstats_error.py", line 3, incProfile.run('my_super_slow_routine()') File "/usr/lib/python2.6/cProfile.py", line 36, in run result = prof.print_stats(sort) File "/usr/lib/python2.6/cProfile.py", line 80, in print_stats import pstats ImportError: No module named pstats
Solution ...
... read more »I'm Eliot and this is my notepad for programming topics such as Python, Django, Ubuntu, Emacs, etc... more »