随笔分类 -  Python

摘要:1 #!/usr/bin/env python 2 with open("ha.log",'r',encoding="utf-8") as f,open("ha1.log",'w',encoding="utf-8") as f1: 3 for line in f : 4 f1.write(line) 阅读全文
posted @ 2017-01-26 11:50 失落的黎明 阅读(213) 评论(0) 推荐(0)
摘要:1.close关闭文件 2.flush刷新文件内部缓冲区(强制写入硬盘) 3.read读取指定字节数据 4.readline仅读取一行数据,可循环读下一行 5.seek指定文件中指针位置 tell获取指针位置 6.truncate截断数据,仅保留指定之前数据 7.write写内容 阅读全文
posted @ 2017-01-26 11:16 失落的黎明 阅读(597) 评论(0) 推荐(0)
摘要:1.打开文件时,需要指定文件路径和以何等方式打开文件,打开后,即可获取该文件句柄,日后通过此文件句柄对该文件操作。 打开文件的模式有: r ,只读模式【默认】 w,只写模式【不可读;不存在则创建;存在则清空内容;】 x, 只写模式【不可读;不存在则创建,存在则报错】 a, 追加模式【可读; 不存在则 阅读全文
posted @ 2017-01-26 10:38 失落的黎明 阅读(531) 评论(0) 推荐(0)
摘要:1 #!/usr/bin/env python 2 a = 10/3 3 print(a) 4 #divmod计算商与余数 5 r = divmod(10001,20) 6 print(r) 7 #eval可以执行一个字符串形式的表达式 8 ret = eval("1 + 3") 9 c = eval("a + 60",{"a": 99}) 10 print(ret) 11 p... 阅读全文
posted @ 2016-12-24 15:42 失落的黎明 阅读(578) 评论(0) 推荐(0)
摘要:1 #!/usr/bin/env python 2 li = [] 3 print(dir(li)) 4 help(list) 阅读全文
posted @ 2016-12-24 15:18 失落的黎明 阅读(138) 评论(0) 推荐(0)
摘要:如果对象object参数是可以调用的对象,就返回True;否则返回False。不过要注意的是,当一个对象是可以调用的,并不表示调用该对象时执行一定成功,但不可调用的对象去调用时一定不会成功。如果类对象是一个类对象,那么这个类是否重载有__call__()方法来决定,如果重载有这个方法就判断为True 阅读全文
posted @ 2016-12-24 14:56 失落的黎明 阅读(425) 评论(0) 推荐(0)
摘要:1 #!/usr/bin/env python 2 import random 3 temp = "" 4 for i in range(6) : 5 num = random.randrange(0, 4) 6 if num == 3 or num == 1 : 7 rad2 = random.randrange(0, 10) 8 ... 阅读全文
posted @ 2016-12-24 14:28 失落的黎明 阅读(264) 评论(0) 推荐(0)
摘要:1、bin() 将整数x转换为二进制字符串,如果x不为Python中int类型,x必须包含方法__index__()并且返回值为integer; 参数x:整数或者包含__index__()方法切返回值为integer的类型; 1 #!/usr/bin/env python 2 r = bin(10) 阅读全文
posted @ 2016-12-24 14:05 失落的黎明 阅读(2180) 评论(0) 推荐(1)
摘要:1 #!/usr/bin/env python 2 #bin() #二进制 3 #oct() #八进制 4 #int() #十进制 5 #hex() #十六进制 6 int(10) 7 i = int('0b11',base=2) 8 print(i) 9 r = int('11',base=8) 阅读全文
posted @ 2016-12-24 13:08 失落的黎明 阅读(200) 评论(0) 推荐(0)
摘要:1 #!/usr/bin/env python 2 #all循环参数,如果每个元素都为真,那么all的返回值为真 3 r = all([True,'sad','asd']) 4 print(r) 5 #any 只有一个真,则为真 6 i = any(['as','asd','']) 7 print( 阅读全文
posted @ 2016-12-24 12:54 失落的黎明 阅读(176) 评论(0) 推荐(0)
摘要:1 #!/usr/bin/env python 2 def fun(arg) : 3 ret = [] 4 for i in range(len(arg)) : 5 if i % 2 ==1 : 6 ret.append(arg[i]) 7 else : 8 pass 9 r... 阅读全文
posted @ 2016-12-24 12:24 失落的黎明 阅读(420) 评论(0) 推荐(0)
摘要:1 #!/usr/bin/env python 2 def fun(arg) : 3 if len(arg) > 2 : 4 return arg[0:2] 5 return arg 6 def fun1(arg) : 7 if len(arg) > 2 : 8 return arg[2:] 9 r 阅读全文
posted @ 2016-12-24 12:14 失落的黎明 阅读(93) 评论(0) 推荐(0)
摘要:1 #!/usr/bin/env python 2 def has_space(args) : 3 ret = True 4 for c in args : 5 if c.isspace() : 6 ret = False 7 break 8 return ret 9 ret = has_s... 阅读全文
posted @ 2016-12-24 12:10 失落的黎明 阅读(274) 评论(0) 推荐(0)
摘要:1 #!/usr/bin/env python 2 def obj_len(arg) : 3 if isinstance(arg,str) or isinstance(arg,list) or isinstance(arg, tuple) : 4 if len(arg) >5 : 5 return True 6 else... 阅读全文
posted @ 2016-12-24 12:01 失落的黎明 阅读(585) 评论(0) 推荐(0)
摘要:斐波那契数列指的是这样一个数列 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368... 阅读全文
posted @ 2016-12-23 18:57 失落的黎明 阅读(546) 评论(0) 推荐(0)
摘要:1、返回值 函数是一个功能块,该功能到底执行成功与否,需要通过返回值来告知调用者。 以上要点中,比较重要有参数和返回值: 2、参数 为什么要有参数? 1 def CPU报警邮件() 2 #发送邮件提醒 3 连接邮箱服务器 4 发送邮件 5 关闭连接 6 7 def 硬盘报警邮件() 8 #发送邮件提 阅读全文
posted @ 2016-12-23 18:50 失落的黎明 阅读(382) 评论(0) 推荐(0)
摘要:一、数字和字符串 对于 数字 和 字符串 而言,赋值、浅拷贝和深拷贝无意义,因为其永远指向同一个内存地址。 二、其他基本数据类型 对于字典、元祖、列表 而言,进行赋值、浅拷贝和深拷贝时,其内存地址的变化是不同的。 1、赋值 赋值,只是创建一个变量,该变量指向原来内存地址,如: 2、浅拷贝 浅拷贝,在 阅读全文
posted @ 2016-12-23 18:29 失落的黎明 阅读(145) 评论(0) 推荐(0)
摘要:set集合,是一个无序且不重复的元素集合 阅读全文
posted @ 2016-12-23 18:23 失落的黎明 阅读(1712) 评论(0) 推荐(0)
摘要:三元运算(三目运算),是对简单的条件语句的缩写 阅读全文
posted @ 2016-12-23 18:21 失落的黎明 阅读(179) 评论(0) 推荐(0)
摘要:1 def jisuan(x) : 2 al_num = 0 3 spance_num = 0 4 digit_num = 0 5 other_num = 0 6 for i in x : 7 if i.isdigit() : 8 digit_num += 1 9 elif i.is... 阅读全文
posted @ 2016-12-23 18:00 失落的黎明 阅读(363) 评论(0) 推荐(0)