跳到主要内容

python中的中文编码问题

· 阅读需 2 分钟

在编程领域中,中文编码问题一向是个很头疼的问题,今天写点总结一下解决的方法

unicode与utf-8和gbk之间的转换,涉及到的函数有decode与encode

首先明确一下,python中的字符串都是以某种编码来存储的,就中文来说,以gbk与utf-8的,你虽然可以这样定义一个变量 s = u’杨彦星’,但是你可以用type(s) 来看一下它的类型,此时这个s 不是字符串,而是unicode类型,当你定义s = ‘杨彦星’的时候,这时type(s)才是str类型,但是当你直接定义s = ‘杨彦星’时会在不同的场景下出现不同的问题,windows下的cmd还好,换到一个utf-8的解析器里就是乱码,所以最好的方式是在定义有中文(或者说非英文的)字符串时以unicode来定义,然后再去解码输出。

将字符串decode成unicode

s = ‘杨彦星’

s_uni = s.decode(‘gbk’) #将s转换成unicode,decode的时候就加上这个字符原来的编码类型

s_utf8 = s_uni.encode(‘utf-8’)

s_gbk = s_uni.encode(‘gbk’)

web.py中关于url的总结

· 阅读需 3 分钟

网页中的数据在传递的时候有GET和POST两种方式,GET是以网址的形式传参数,在web.py中有着很好的匹配,如果我们配置以下的urls

  urls =(
'/','index',
'/weixin/(.*?)','WeixinInterface'

先不考虑/weixin/后面的东西,现在我们来写index的类

class index:
def GET(self):
i = web.input(name = 'kevinkelin',age = 100)

在中文windows下使用pywinauto进行窗口操作(一)

· 阅读需 6 分钟

这两天开始接触pywinauto,听说百度的自动化QA也用这个模块,于是来了兴趣,但网上的教程很少,而且基本上都是拿官方的notepad来说,首先中文菜单的支持是问题,其次各种操作也没有写清楚,阅读官方的文档,发现这个东西使用起来还真是非常方便,下面我也以notepad为例来说明一下它的简单操作。

pywinauto;

https://sourceforge.net/project/showfiles.php?group_id=157379

Sendkeys

http://www.rutherfurd.net/python/sendkeys/index.html

ctypes (如果你是python2.3或者2.4)

检测你是否安装正确

from pywinauto import application app = application.Application.start("notepad.exe") app.notepad.TypeKeys("%FX")

使用python进行windows自动化测试(1)

· 阅读需 6 分钟

最近开始学习整理python在windows自动化测试中的使用,觉得挺有意思的

主要思路,在windows下,主要通过启进程,然后查找进程的句柄,然后再操作这个句柄,包括点击,填写文字,关闭,获取文字等操作

下面以一个简单的校验文件md5值的操作来介绍一个python的应用,当然python中有校验md5的函数,不用非要使用工具来校验,这里只是练习使用python来自动化操作

所用的工具有SpyLite,用于查看窗口ID,句柄等信息

Python中的urllib2库的使用

· 阅读需 4 分钟

今天研究了下urllib2这个库的使用,才发现以前有很多不明白的东西,现在写下来也做个记录

import urllib2
url = r'http://www.baidu.com'
html = urllib2.urlopen(url).read()

客户端与服务器端通过request与response来沟通,客户端先向服务端发送request,然后接收服务端返回的response

urllib2提供了request的类,可以让用户在发送请求前先构造一个request的对象,然后通过urllib2.urlopen方法来发送请求

import urllib2
url = r'http://www.baidu.com'
req = urllib2.Request(url)
html = urllib2.urlopen(req).read()

python解析json

· 阅读需 5 分钟

相对于python解析XML来说,我还是比较喜欢json的格式返回,现在一般的api返回都会有json与XML格式的选择,json的解析起来个人觉得相对简单些

先看一个简单的豆瓣的图书查询的api返回

Python解析XML文档

· 阅读需 6 分钟

解析XML主要用到pytohn自带的XML库,其次还是lxml库

先以一个相对简单但功能比较全的XML文档为例

<?xml version='1.0' encoding='utf-8'?>
<feed xmlns='http://www.w3.org/2005/Atom' xml:lang='en'>
<title>dive into mark</title>
<subtitle>currently between addictions</subtitle>
<id>tag:diveintomark.org,2001-07-29:/</id>
<updated>2009-03-27T21:56:07Z</updated>
<link rel='alternate' type='text/html' href='http://diveintomark.org/'/>
<entry>
<author>
<name>Mark</name>
<uri>http://diveintomark.org/</uri>
</author>
<title>Dive into history, 2009 edition</title>
<link rel='alternate' type='text/html'
href='http://diveintomark.org/archives/2009/03/27/dive-into-history-2009-edition'/>
<id>tag:diveintomark.org,2009-03-27:/archives/20090327172042</id>
<updated>2009-03-27T21:56:07Z</updated>
<published>2009-03-27T17:20:42Z</published>
<category scheme='http://diveintomark.org' term='diveintopython'/>
<category scheme='http://diveintomark.org' term='docbook'/>
<category scheme='http://diveintomark.org' term='html'/>
<summary type='html'>Putting an entire chapter on one page sounds
bloated, but consider this &mdash; my longest chapter so far
would be 75 printed pages, and it loads in under 5 seconds&hellip;
On dialup.</summary>
</entry>
<entry>
<author>
<name>Mark</name>
<uri>http://diveintomark.org/</uri>
</author>
<title>Accessibility is a harsh mistress</title>
<link rel='alternate' type='text/html'
href='http://diveintomark.org/archives/2009/03/21/accessibility-is-a-harsh-mistress'/>
<id>tag:diveintomark.org,2009-03-21:/archives/20090321200928</id>
<updated>2009-03-22T01:05:37Z</updated>
<published>2009-03-21T20:09:28Z</published>
<category scheme='http://diveintomark.org' term='accessibility'/>
<summary type='html'>The accessibility orthodoxy does not permit people to
question the value of features that are rarely useful and rarely used.</summary>
</entry>
<entry>
<author>
<name>Mark</name>
</author>
<title>A gentle introduction to video encoding, part 1: container formats</title>
<link rel='alternate' type='text/html'
href='http://diveintomark.org/archives/2008/12/18/give-part-1-container-formats'/>
<id>tag:diveintomark.org,2008-12-18:/archives/20081218155422</id>
<updated>2009-01-11T19:39:22Z</updated>
<published>2008-12-18T15:54:22Z</published>
<category scheme='http://diveintomark.org' term='asf'/>
<category scheme='http://diveintomark.org' term='avi'/>
<category scheme='http://diveintomark.org' term='encoding'/>
<category scheme='http://diveintomark.org' term='flv'/>
<category scheme='http://diveintomark.org' term='GIVE'/>
<category scheme='http://diveintomark.org' term='mp4'/>
<category scheme='http://diveintomark.org' term='ogg'/>
<category scheme='http://diveintomark.org' term='video'/>
<summary type='html'>These notes will eventually become part of a
tech talk on video encoding.</summary>
</entry>
</feed>

先简单的看一下这个XML的结构