摘要: 模拟 http 请求是比较常见的一种需求,在 Python 中,使用 http 模块操作。 1 import http.client 2 3 # 创建 Http 连接。 4 http = http.client.HTTPConnection('www.baidu.com')... 阅读全文
posted @ 2014-09-27 23:45 h82258652 阅读(1088) 评论(0) 推荐(0) 编辑
摘要: 在某些情况下,我们需要定义自己的异常并且抛出先定义一个错误:1 class MyError(BaseException):2 def __init__(self):3 pass上面定义了一个叫MyError的类,继承自BaseException。在Python中,所有的错误都... 阅读全文
posted @ 2014-09-27 23:06 h82258652 阅读(175) 评论(0) 推荐(0) 编辑
摘要: 错误是多种多样的,在 except 语句中,可以捕获指定的异常修改代码如下: 1 import io 2 3 path = r'' 4 mode = 'w' 5 6 try: 7 file = open(path,mode) 8 str = file.read() 9 p... 阅读全文
posted @ 2014-09-27 22:59 h82258652 阅读(183) 评论(0) 推荐(0) 编辑
摘要: 虽然叫错误,但跟 C# 中的异常是一回事。只不过 Python 中叫错误(Error)而 C# 中叫异常(Exception)。先手工产生一个异常:1 file = open('','r')上面一句由于路径是空路径,因此文件肯定是不存在的,执行这一句会引发 FileNotFoundError 这个错... 阅读全文
posted @ 2014-09-27 22:42 h82258652 阅读(217) 评论(0) 推荐(0) 编辑
摘要: Python应用最广泛的要数web方面了。因此,socket显得十分重要。要使用socket,必须引入socket模块,因此在Python脚本开头先写入1 import socket学过socket的都知道,就是客户端和服务端的通信。因此新建client.py和server.py文件。先编写serv... 阅读全文
posted @ 2014-09-27 22:26 h82258652 阅读(451) 评论(0) 推荐(0) 编辑
摘要: 在Python中写文件也是得先打开文件的。1 file=open(r'E:\temp\test.txt','a')2 file.write('append to file')3 file.close()第一行使用追加模式打开文件,第二行则将内容写入文件,第三行关闭文件。除了write方法外,还有wr... 阅读全文
posted @ 2014-09-27 17:19 h82258652 阅读(231) 评论(0) 推荐(0) 编辑
摘要: 在Python中,读取文件使用open函数1 file=open(r'E:\temp\test.txt','r')2 var = file.read()3 print(var)4 file.close()第一行打开E:\temp\test.txt文件,注意字符串开头使用了r,r表明字符串内的内容不转... 阅读全文
posted @ 2014-09-27 17:12 h82258652 阅读(220) 评论(0) 推荐(0) 编辑
摘要: 在Python中,输出使用print函数,之前用过了。输入的话,则使用input函数。1 var = input()2 print('you input is' + var)输入haha则将输出you input is haha。可见input的作用与C#中的Console.ReadLine方法一样... 阅读全文
posted @ 2014-09-27 16:58 h82258652 阅读(204) 评论(0) 推荐(0) 编辑
摘要: Python是一门面向对象语言,那么作为面向对象的特征——类也是有的。值得注意的是Python中一切皆对象,并不像C#中为了性能考虑,int这些在Python中也是对象。(C#中int是结构体)如何定义一个类:1 class Person:2 pass使用class关键字,上面定义了一个Pe... 阅读全文
posted @ 2014-09-27 16:46 h82258652 阅读(338) 评论(0) 推荐(0) 编辑
摘要: 在Python中定义函数的时候,可以使用参数默认值的方式定义函数例子:1 def welcome(who,state='is',action='talking'):2 print(who,state,action)调用函数:1 welcome('Tom')输出Tom is talking1 ... 阅读全文
posted @ 2014-09-27 14:11 h82258652 阅读(171) 评论(0) 推荐(0) 编辑
摘要: 先定义一个最基本的函数作为例子:1 def Print(msg):2 print(msg)函数名为Print,参数有一个,为msg,函数体调用print系统函数,输出msg。接下来就是可变参数,这个特性是比较特殊的,像C#中的params,但又有所不同。例子1:1 def PrintTupl... 阅读全文
posted @ 2014-09-27 13:55 h82258652 阅读(292) 评论(0) 推荐(0) 编辑
摘要: 例子:1 i = 12 while i < 10:3 print(i)4 i+=15 else:6 print('finish')输出1至9和finish在while语句中同样支持for语句所支持的continue、break和else 阅读全文
posted @ 2014-09-27 13:28 h82258652 阅读(195) 评论(0) 推荐(0) 编辑
摘要: Python中循环可以使用for语句来实现1 list = ['Tom','Lucy','Mary']2 for name in list:3 print(name)则将会依次输出Tom Lucy Mary另外Python还支持continue和break关键字,用法与C#相同。比较有特点的... 阅读全文
posted @ 2014-09-27 13:14 h82258652 阅读(380) 评论(0) 推荐(0) 编辑
摘要: Python的分支语句比较简单,只有if、else、elif三个关键字,也就是说,Python没有switch语句,而且,Python中并没有?:这个三目运算符。例子:1 age = 182 if age < 18:3 print('too young')4 elif age == 18:5... 阅读全文
posted @ 2014-09-27 01:09 h82258652 阅读(493) 评论(0) 推荐(0) 编辑