urllib2.urlopen方法的url中文编码问题

在urllib2.urlopen的文档中并没有提及url编码的问题,但实际使用中肯定会发现当url包含中文时会有编码异常

在实验多次之后发现只需要将传入urlopen的url编码为utf-8即可,不能是unicode哦,也不需要再做unquote处理

Posted in Python | Tagged | Leave a comment

从Mac中删除Google Software Update

之前安装了个GAE,结果经常提示更新,把GAE删了之后Google Software Update还在,要删除Google Software Update在终端里面执行下面命令即可

~/Library/Google/GoogleSoftwareUpdate/GoogleSoftwareUpdate.bundle/Contents/Resources/GoogleSoftwareUpdateAgent.app/Contents/Resources/install.py --uninstall

sudo /Library/Google/GoogleSoftwareUpdate/GoogleSoftwareUpdate.bundle/Contents/Resources/GoogleSoftwareUpdateAgent.app/Contents/Resources/install.py --uninstall
Posted in Mac OS X | Tagged | Leave a comment

readability的python实现

readability能获取大部份网页的主要内容,这个算法很cool,学习了一下,就写了个python版的实现,之前找过python版的实现,但都是比较老的基于0.4的,现在都1.7.1了

写了之后就有人给我要这个代码,放github给大家用吧

https://github.com/kingwkb/readability

demo: http://yanghao.org/tools/readability

Posted in Python | Tagged | 1 Comment

ubuntu常用软件

用ubuntu也有一些时间了,常用软件记录下

  • 青松二笔输入法(ibus)
  • gftp
  • pycharm
  • netbeans
  • vlc
  • kiki
  • google chrome
  • dropbox
  • gimp
  • remmina
  • rapidsvn

自带的不就写了,足够满足工作,简单娱乐需求,不过操作体验gnome还是和mac有一定差距的。

Posted in Linux | Leave a comment

好久没写点内容了

看了看时间,有8个月没更新,比较忙、比较忙,这也不能算理由吧,文字不好,以后要多锻炼。

Posted in MyBlog | Leave a comment

文本编码“简体中文 (Mac OS)”不适用

未能打开文稿“xxxxxx.txt”。文本编码“简体中文 (Mac OS)”不适用。

该文件可能已使用了不同的文本编码来存储,或者可能不是文本文件。

如果你用Mac这些文字一定并不陌生吧。

当你在mac os打开windows拷贝过来的文件时很可以出现这种问题,当然也不是全部,还得满足一个条件,就是这个文件在windows上保存的时候选择的是gbk编码。

这个问题其实应该算是mac的一个bug,不知道mac为什么一直不修复,在mac上面有2种中文编码,一种是和windows上面的中文编码一样的中文(GB 18030)此编码是中文gbk编码的一种扩展,所以完全兼容gbk,但是除了这个之外mac还另外搞了一种中文编码叫简体中文(Mac OS),此编码和gbk还有GB 18030是不一样;当你打开gbk编码的文件时,mac识别成了简体中文(Mac OS),所有就打不开喽。

解决办法:

打开文本编辑的偏好设置,选择打开和存储选项卡,在下面的纯文本文件编码 下面的打开文件后面选择中文(GB 18030)就可以了。

下面是google到的内容

如何将 Macintosh 简体中文编码转换成相应的 GB 标准?
简体中文的 Mac 编码是一种移位的 GB2312。从 GB2312 转成 Mac 编码, 每个字符要加上0×8080。 相反从 Mac 编码转成 GB2312, 则每个字符都减去 0×8080。
字符集西方字符区域不能加减。下面的例子说明了如何进行这种转换。

// 如字符需要转换则返回true,反之单字节字符则返回false(意味着只处理了首字节)
// (i.e. false 表示是西方字符)
boolean MacToGB2312(unsigned char first, unsigned char second,
    unsigned short *output)
{
    if (first < 0x81) {
        *output = first;
        return false;
    } else {
        unsigned short temp;
        temp = (first - 0x80) << 8;
        temp += (second - 0x80);
        *output = temp;
        return true;
    }
}
// 总是转换, 无需单独获取字节,无需返回是否转换
void GB2312ToMac(unsigned short input, unsigned short *output)
{
    *output = input + 0x8080;
}

从代码中可以看出, 需要移动双字节字符的每个字节。这样确定一个字符是双字节字符的一部分还是单字节西方字符就显而易见了。

Posted in Mac OS X | Tagged | 1 Comment

web.py 0.36发布

距离0.35还没多久就发布了0.36,此版本主要修复0.35的问题,更新内容如下:

* Upgraded to CherryPy WSGIServer 3.2.0. — #66
* Various Jython compatibility fixes (tx Ben Noordhuis)
* allow strips to accept lists — #69
* Improvements to setcookie (tx lovelylain) — #65
* Added __contains__ method to Session. (tx lovelylain) #65
* Added secure option to session. — #38
* Fixed db.delete error with `using` clause (tx berndtj) — #28
* Fixed the case of no where-clauses in db.where
* Fixed threadlocal error in python2.3 — #77
* Fixed TemplateResult inconsistant behavior — #78
* Fixed query execution issues with MSSQL — #71

Posted in Python | Tagged | Leave a comment

Ambiguous output redirect

在FreeBSD中使用command > file 2>&1时候会得到这个错误
Ambiguous output redirect

出错的原因在于Freebsd默认使用csh,在csh中如果想把标准输出和错误输出同时重定向到一个文件,需要用下面命令
command >& file

Posted in FreeBSD | Leave a comment

web.py升级到0.35

本地升级了,简单的看了一下,之前的程序还未发现在0.35下有问题的情况,另外更新日志中所说的新的表单样式,我还未发现新的Form的render和老的有什么区别。

Posted in Python | Tagged | Leave a comment

web.py 0.35发布

ChangeLog

  • Better ThreaedDict implementation using threadlocal (tx Ben Hoyt)
  • Make Form a new-style class — #53
  • Optimized SQLQuery.join and generation of multiple_insert query — #58
  • New: support for Amazon’s Simple Email Service
  • Added httponly keyword to setcookie (tx Justin Davis)
  • Added httponly only option to sessions and enabled it by default (tx Justin Davis)
  • made htmlquote and htmlunquote work with unicode
  • Added doseq support for web.url
  • New flag web.config.debug_sql to control printing of db queries (tx Nimrod S. Kerrett)
  • Fixed inserting default values into MySQL — #49
  • Fixed rendering of Dropdown with mutliple values (tx krowbar) — #43
  • Fixed mutliple set-cookie header issue with session — #45
  • Fixed error in safeunicode when used with appengine datastore objects
  • Fixed unicode error in generating debugerror — #26
  • Fixed GAE compilation issue — #24
  • Fixed unicode encoding issue in templates — #17
  • Fixed a bug in form.RadioButton when called with tuple options (tx fhsm) — #13
  • Fixed error in creating PostgresDB with pgdb driver (tx cninucci) — #23
  • Support auto convertion of timestamp/date datatypes in sqlite to datetime.data objects — #22
  • Fixed escaping issue on GAE — #10
  • fixed form.validates for checkbox (tx Justin Davis).
  • fixed duplicate content-type in web.sendmail — #20
  • Fix: create session dirs if required (tx Martin Marcher)
  • Fixed safestr to make use of encoding argument (tx s7v7nislands)
  • Don’t allow /static/../foo urls in dev webserver (tx Arnar Lundesgaard)
  • Disabled debug mode in flup server (tx irrelative) — #35
  • And a lot of unicode fixes

值得注意的有这几点

  • 表单使用新的样式
  • cookie增加httponly
  • 修复模版中unicode编码问题
  • 修复很多unicode问题
Posted in Python | Tagged | Leave a comment