大头
Table_bottom

标签云
Table_bottom

分类
Table_bottom

日历
二月
28293031123
45678910
11121314151617
18192021222324
252627282912
Table_bottom

评论
Table_bottom

留言
Table_bottom

微博
Table_bottom

热门文章
Table_bottom

随机文章
Table_bottom

豆瓣上谁关注这里
Table_bottom

链接
Table_bottom

搜索栏
Table_bottom

RSS
RSS Link
Table_bottom

功能
Table_bottom

页面
Table_bottom

计数器
462737
Table_bottom

访客统计
Table_bottom

存档
Table_bottom

python笔记

BeautifulSoup

how to get the address(href=) <a></a> tag refer to.

from BeautifulSoup import BeautifulSoup 
print soup('a')[0]['href'] # equivalent to soup.findAll('a')

如果用find或者findNext,返回的不是列表,则不需要[0]。

Python Issue 1521491

遇到一个win xp+python2.5.1/python2.6.4上'a+'模式读写文件的bug,写文件时报错:IOError: [Errno 0] Error。stackoverflow上搜到讨论,该bug(Python Issue Tracker上的页面)似乎在win xp + python 2.5/python2.6上都会出现,这里有讨论如何避开该bug:

this is pilot error, inherited from the limitations of C I/O: the effect of mixing reads with writes on a file open for update is entirely undefined unless a file-positioning operation occurs between them (for example, a seek()).

在f.read()和f.write()间插入f.seek(f.tell())。

acc_file = open('bla_path', 'at+')
acc_str = acc_file.read()

acc_file.seek(acc_file.tell())
acc_file.write('bla')

PyInstaller

.py文件复制到python目录下(譬如Python2.5.1),在这个目录下运行:

> python <PyInstaller Dir>/Makespec.py --onefile your_prog.py
> python <PyInstaller Dir>/Build.py your_prog.spec

该目录dist下会生成名为your_prog的目录。创建.spec文件时的参数可查询pyinstaller-1.5\doc\Manual.html。

参考介绍:PyInstaller使用简介

Unicode

很好的presentation:Unicode in Python

Unicode HOWTO

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)的解决方法。

pyExcelerator