博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python打卡第三周
阅读量:5151 次
发布时间:2019-06-13

本文共 6383 字,大约阅读时间需要 21 分钟。

第一天

  1. GBK
  2. UTF-8
  3. 格式转换

 

从默认的utf-8转换成gbk

s = "你好"s_gbk = s.encode("gbk")print(s_gbk)print(s.encode())gbk_to_utf8 = s_gbk.decode("gbk").encode("utf-8")print("utf8",gbk_to_utf8)

 

 

调用sys模块,查看当前的编码,解码成gbk,再重新编码成utf-8

import sysprint(sys.getdefaultencoding())s = u"你好"print(s)#s_to_unicode = s.decode("utf-8")s_to_gbk = s.encode("gbk")print(s_to_gbk)#print(s_to_unicode)gbk_to_utf8 = s_to_gbk.decode("gbk").encode("utf-8")print(gbk_to_utf8)

 

utf-8转换成gb2312不能直接转,需要通过gbk中转

s = "你哈"print(s.encode("gbk"))print(s.encode("utf-8"))print(s.encode("utf-8").decode("utf-8").encode("gb2312").decode("gb2312"))

 

小结:编码转换的关键在于,utf-8和gb2312不能直接转换,需要通过中间编码(gbk)进行中转

 

第二天

  1. 文件的读写
  2. 循环输出文件
  3. 找到文件的具体位置
  4. 刷新
  5. with语句

seek():移动文件读取指针到指定位置

tell():返回文件读取指针的位置

seek()的三种模式:

    (1)f.seek(p,0)  移动当文件第p个字节处,绝对位置

    (2)f.seek(p,1)  移动到相对于当前位置之后的p个字节

    (3)f.seek(p,2)  移动到相对文章尾之后的p个字节

#data=open("yesterday",encoding="utf-8").read()f = open("yesterday2",'a',encoding="utf-8")f.write("\nwhen i was a young man i paly coumpter\n")data=f.read()f.close''''''f=open("yesterday",'r+',encoding="utf-8")print(f.readline())print(f.readline())print(f.readline())f.write("---------diao-------")print(f.readline())''''''f = open("yesterday2","w+",encoding="utf-8")f.write("---------diao-------\n")f.write("---------diao-------\n")f.write("---------diao-------\n")print(f.tell())f.seek(10)#找到第十行print(f.tell())print(f.readline())f.write("print test file!")f.close()'''f = open("yesterday2","wb")#二进制f.write("hello binary\n".encode())f.close()'''print(f.tell())print(f.readline())print(f.readline())print(f.readline())print(f.tell())f.seek(10)#转到第十行print(f.readline())print(f.encoding)print(f.fileno())print(f.flush())#实时刷新print(dir(f.buffer))'''#f.truncate(20)#截断20个,使用a追加#for i in range(5):  #  print(f.readline())#print(f.readlines())#high bige'''count = 0for line in f:    if count == 9:        print('------ 我是分割线------')        count +=1        continue    print(line)#效率最高    count+=1'''#low loop'''for index,line in enumerate(f.readlines()):    print(line.strip())    if index == 9:        print('-----我是分割线-----')        continue'''

 

利用刷新制作进度条

import sys,timefor i in range(20):    sys.stdout.write("#")    sys.stdout.flush()    time.sleep(0.1)

 

利用sys模块 进行替换内容

import sysf = open("yesterday","r",encoding="utf-8")f_new =open("yesterday.bak","w",encoding="utf-8")find_str = sys.argv[1]replace_str = sys.argv[2]for line in f:    if find_str in line:        line = line.replace(find_str,repalce_str)    f_new.write(line)f.close()f_new.close()

 

同时打开两个文件

with open("yesterday","r",encoding="utf-8") as f,\     open("yesterday2","r",encoding="utf-8") as f2:    for line in f:        print(line)

 

 

第三天

  1. 集合的交并补
  2. 集合的添加

交集:  list_1.intersection(list_2)   或者list_1 & list_2

并集:  list_1.union(list_2)   或者 list_1 || list_2

差集:  list_1.difference(list_2)   或者 list_1 - list_2

子集:  list_1.issubset(list_2)

父集:  list_1.issuperset(list_2)

对称差集:list_1。symmetric_difference(list_2)   或者  list_1 ^ list_2

 

list_1=[1,5,3,7,9,5,7]list_1=set(list_1)list_2=set([2,5,0,22,8,66,5,7])#无序#print(list_1,type(list_1))print(list_1,list_2)#交集print(list_1.intersection(list_2))#并集print(list_1.union(list_2))#差集print(list_1.difference(list_2))print(list_2.difference(list_1))#子集/父集list_3=set([1,3,7])print(list_1.issubset(list_2))print(list_1.issuperset(list_2))print(list_3.issubset(list_1))#对称差集print(list_1.symmetric_difference(list_2))print("---------")list_4=set([5,6,8])print(list_3.isdisjoint(list_4))#把list4加到list3中#intersectionprint(list_1&list_2)#unionprint(list_1|list_2)#differenceprint(list_1-list_2)#对称差集print(list_1^list_2)list_1.add(999)list_1.update([100,111,222])print(list_1)print(list_1.pop())list_1.remove("ddd")list_1.discard()

 

 

第四天

  1. 函数的定义
  2. 实参
  3. 形参
  4. 调用赋值

 

最基本的函数定义和输出

#函数def func1():    """testing1"""    print('in the func1')    return 0#过程def func2():    '''testing2'''    print('in the func2')x = func1()y = func2()print('from func1 return is %s'%x)print('from func2 return is %s'%y)

 

使用time模块,按时间格式输出函数,用元组的方式返回多个值

import timedef logger():    time_format = '%Y-%m-%d %X'    time_current = time.strftime(time_format)    with open('a.txt','a+') as f:        f.write('%s end action\n' %time_current)def test1():    print('in the test1')    #logger()def test2():    print('in the test2')    #logger()    return 0def test3():    print('in the test3')    #logger()    return 1,'hello',['a','b','c'],{
'name':'Louis'}t1=test1()t2=test2()t3=test3()print(t1)print(t2)print(t3)

 

关键字调用分为

  位置关键字——直接输入值

  关键关键字——使用赋值语句,但是得写在位置参数后面

def test(x,y,z):    print(x)    print(y)    print(z)#test(y=2,x=1)#关键字调用,与形参顺序无关#test(1,2)#位置关键字#test(x=2,3)test(3,z=2,y=6)#关键参数不能写在位置参数前面

 

默认参数赋值,

def test(x,y=2):    print(x)    print(y)test(1)#默认参数特点:调用函数的时候,默认参数可有可无'''用途:1.默认安装值def test(x,sotf1=True,soft2=True):       2.数据库端口号def conn(host,port==3306):pass'''

 

小结:使用def定义函数,然后单独调用输出,返回return 0或者1,也可以返回元组。使用关键字参数赋值的时候,关键字参数得在位置关键字的后面

 

第五天

  1. args参数
  2. kwargs参数
  3. 全局变量和局部变量

 

1.*args的功能:------接收N个位置参数,转换成元组tuple形式

2.**kwargs的功能------接收N个关键字参数,转换成字典dict形式

3.位置参数一定要在关键字参数之前,也就是(*args,**kwargs)

 

def test(*args):    print(args)test(1,2,3,4,5)test(*[1,2,3,4,5])# args=tuple([1,2,3,4,5])'''def test1(x,*args):    print(x)    print(args)test1(1,2,3,4,5,6,7)'''def test2(**kwargs):    print(kwargs)    print(kwargs['name'])    print(kwargs['age'])    print(kwargs['sex'])#把n个关键字转换成字典存储test2(name='louis',age=18,sex='M')#test2(**{'name':'louis','age':8})def test3(name,**kwargs):    print(name)    print(kwargs)test3('louis',age=18,sex='M')def test4(name,age=18,*args,**kwargs):    print(name)    print(age)    print(args)    print(kwargs)    logger("TEST4")#args:接受n个位置参数,转换成元组的形式def logger(source):    print("from %s"% source)test4('louis',age=34,sex='M',hobby='tesla')

 

全局变量的值在局部变量里能使用,局部变量的值出了局部变量就不能使用

在函数外不能修改函数内的值

school = "WuDa"names = ["louis","jack","Tom"]name_tuple = (1,2,3,4)def change():    names[0]="枫叶"    name_tuple=(2,3,4,5)    print("inside func",names)    print(name_tuple)change()print(name_tuple)#print(names)# def change_name(name):#     global school#     school = "XDF"#     print("before change ",name,school)#     name = "louis"#     print("after change",name)# name = "Louis"# change_name(name)# print(name)# print("School:",school)

 

 

第六天

  1. 递归调用
  2. 高阶函数

 

递归调用必须得有结束的地方,要不然会变成死循环一直消耗内存

def calc(n):    print(n)    if int(n/2)>0:        return calc(int(n/2))    print("->",n)calc(10)

 

绝对值相加,高阶函数

def add(a,b,f):    return f(a)+f(b)res = add(3,-6,abs)print(res)

 

转载于:https://www.cnblogs.com/yuanjun93/p/10700230.html

你可能感兴趣的文章