Skip to content

Instantly share code, notes, and snippets.

>>> a = "你好"
>>> a
'\xe4\xbd\xa0\xe5\xa5\xbd'
>>> b = u"你好"
>>> b
u'\u4f60\u597d'
>>> print a
你好
>>> print b
你好
@HQMIS
HQMIS / gist:4490944
Created January 9, 2013 05:41
error
Warning (from warnings module):
File "C:\GitHub\FixedPointOfSimsimi\fpos.py", line 24
count = count+1 if answer==question or answer==temp else 0
UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
不同编码方式的变量就行了 == 比较
@HQMIS
HQMIS / gist:4468718
Created January 6, 2013 17:10
utf8转换为gb2312报错, 原因:gb2312对一些很生僻的中文字并不支持,比如 “囧” “苶”
Python 2.7.2 |EPD 7.2-2 (32-bit)| (default, Sep 14 2011, 11:02:05) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> err = u'\u6c39'
>>> print err
>>> err.encode("gb2312")
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
err.encode("gb2312")
@HQMIS
HQMIS / gist:4411289
Created December 30, 2012 06:40
AndroidViewClient 使用记录
#! /usr/bin/env monkeyrunner
# Script Name: androidviewclient.py
# Author Email: huangqimin@baidu.com
# Note:
# 1、 主页:
# https://github.com/dtmilano/AndroidViewClient
# 2、 例子
# https://github.com/dtmilano/AndroidViewClient/blob/master/AndroidViewClient/examples/envsetup.sh
@HQMIS
HQMIS / gist:4408574
Last active December 10, 2015 08:38
【Python】【Library】【chardet】
1、模块作用
  chardet模块用来实现字符串/文件编码检测
2、chardet下载与安装
  下载地址:http://pypi.python.org/pypi/chardet/
  下载chardet后,解压chardet压缩包,直接将chardet文件夹放在应用程序目录下,就可以使用import chardet开始使用chardet了。
  也可以将chardet拷贝到Python系统目录下,这样你所有的python程序只要用import chardet就可以了。
  Python第三方模块中一般会自带setup.py文件,在CMD里切换目录至chardet,然后执行:python setup.py install。
  如果执行上述命令,报错:ImportError: No module named setuptools,则要进行setuptools的安装。
@HQMIS
HQMIS / gist:4405267
Last active December 10, 2015 08:08
Python 编码, 研究
For IDLE
Python 2.7.2 |EPD 7.2-2 (32-bit)| (default, Sep 14 2011, 11:02:05) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import sys
>>> sys.getdefaultencoding()
'ascii'
>>> str_Chinese = "电源管理"
>>> print "".join(["%"+str(hex(ord(x)))[2:] for x in str_Chinese.decode("gb2312").encode("utf8")])
%e7%94%b5%e6%ba%90%e7%ae%a1%e7%90%86
@HQMIS
HQMIS / gist:4337738
Created December 19, 2012 15:52
BYHH 发帖抓包
Request URL:http://byhh.net/cgi-bin/bbssnd?board=test&file=
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Charset:GBK,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:zh-CN,zh;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
@HQMIS
HQMIS / gist:4326422
Created December 18, 2012 09:07
【Python】【Library】【json】
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import json
>>> json_str = "{'json':'str'}"
>>> json.loads(json_str)
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
json.loads(json_str)
File "E:\Python27\lib\json\__init__.py", line 326, in loads
@HQMIS
HQMIS / gist:4324135
Created December 18, 2012 01:23
【Python】【Library】【webbrowser】
FUNCTIONS
get(using=None)
Return a browser launcher instance appropriate for the environment.
open(url, new=0, autoraise=True)
open_new(url)
open_new_tab(url)
@HQMIS
HQMIS / gist:4315795
Created December 17, 2012 04:46
【Python】【Library】【requests】
Python有大量的Standard Library(http://docs.python.org/2/library/),同时也有大量的第三方Library
(http://pypi.python.org/pypi),如此众多的Library,总有一些会给你带来意想不到的惊喜,这里向大家介绍一下,
requests模块(http://pypi.python.org/pypi/requests/0.14.2),可以更方便的进行网络协议相关的处理,大家可
以将该模块看成是,对Python Standard Library urllib2的进一步封装。
官方网址:http://docs.python-requests.org/en/latest/
API列表:http://docs.python-requests.org/
一些人的讨论:https://gist.github.com/973705
Windows安装: