day3

字符转录翻译


 

#Author:walter

s = "米好"

print(s)
print(s.encode("utf-8"))
print(s.encode("utf-8").decode("utf-8")) #转成“utf-8”,然后转成中文
s_gbk = s.encode("gbk") #转化为gbk格式()
print(s_gbk)
s_gbk_utf = s_gbk.decode("gbk").encode("utf-8") #先将gbk翻译为Unicode,然后解释为utf-8格式
print(s_gbk_utf)

打开文件

#Author:walter

#f=open("singsong",'r',encoding = "utf-8") #仅可读模式
#print(f.read())

f = open("singsong",'w',encoding = "utf-8") #仅写模式,如果文件名相同将删除源文件,空白页开始写

f.write("我们都是好孩子,\n")
f.write("我们都是好孩子!!!,\n")


f=open("singsong",'a',encoding = "utf-8") #在后面加写模式attend,也不能读
f.write("这不是真的,\n")
f.write("然而并不是。")
f.flush()
f.close()


##########
'''
f = open("singsong",'r',encoding = "utf-8")
m = f.tell() #告诉光标指针位置
f.seek(0) #返回最初位置
#high loop
count = 0
for line in f: #一行一行读,只保存一行
if count == 2:
print('--------分割线-------')
print(line.strip())
count += 1
continue
print(line.strip())
count += 1
##########
print(f.encoding) #打印字符形式
print(f.fileno()) #文件编号,暂不用
print(f.isatty()) #看是不是什么终端设备
print(f.seekable()) #能否移动光标,能T,否F
print(f.flush()) #刷新到硬盘
print(f.buffer)
f.truncate(100) #截断一部分内容,截到100
#######

#low loop
for index,line in enumerate(f.readlines()):
if index == 3:
print("--------分割线-------")
continue
print(line.strip())

#for i in range(3):
# print(f.readline().strip()) #输出前三行
'''
######
'''
f = open("singsong",'r+',encoding = "utf-8") #读写模式
f = open("singsong",'w+',encoding = "utf-8") #写读模式
f = open("singsong",'a+',encoding = "utf-8") #追加读写模式
f = open("singsong",'rb') #读二进制模式
f = open("singsong",'wb') #写二进制模式
f.close()
'''

###打开自动关闭
import sys

with open("singsong","r",encoding="utf-8",) as f,\
open("singsong2",'r',encoding="utf-8") as f2:
for line in f:
print(line.strip())



修改文件内容

#Author:walter
import sys
'''
f = open("singsong",'r',encoding="utf-8")
file_2 = open("singsong2",'w',encoding="utf-8")
######文件里字符串的修改#########
for line in f:
if "我们都是好孩子" in line:
line = line.replace("我们都是好孩子","可惜不是你")
file_2.write(line)
f.close()
file_2.close()
'''
##########简单的shell sed 替换功能
f = open("singsong",'r',encoding="utf-8")
file_2 = open("singsong2",'w',encoding="utf-8")
find_str = sys.argv[1]
repalce_str = sys.argv[2]
for line in f:
if find_str in line:
line = line.replace("ind_str","可惜不是你")
file_2.write(line)
f.close()
file_2.close()

集合

#Author:walter

list_1 = [1,4,5,7,3,6,7,9]
list_1 = set(list_1)

list_2 = set([2,6,0,66,22,8,4])
print(list_1,list_2)

print(list_2.intersection(list_1)) #取交集(&)

print(list_2.union(list_1)) #取并集 (|)

print(list_2.difference(list_1)) #取差集 #2有的1没有的 (-)

#subset and upperset
print(list_2.issubset(list_2 )) #子集
print(list_2.issuperset(list_1)) #父集

print(list_1.symmetric_difference(list_2)) #对称差集 (^)

print(list_2.isdisjoint(list_1)) #是否没有交集,没有true,有false

list_1.add(999) #添加一项
list_1.update([456,789,132]) #添加多项

list_1.remove()

函数

#Author:walter
def text(x,y):   #形参
print(x)
print(y)

text(y=1,x=2) #与形参顺序无关
text(2,y=1) #与形参一一对应,形参必须灾后
text(1,2) #实参

#Author:walter
import time
def 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\n')
logger()
def test2():
print('in the test2\n')
logger()
def test3():
print('in the test3\n')
logger()

test1()
test2()
test3()

#Author:walter

'''
def test(x,y=2): #默认参数
print(x)
print(y)

test(1,3)
#默认参数特点:调用函数的时候,默认参数非必须传递
#用途:1,默认安装值,连接默认数据库端口号
'''

## *args:接受N位置参数,转化为元组的方式
def test(*args):
print(args)
test(1,2,3,4,5,6,7,8,9)
test(*[1,2,3,4,5,6,7,8]) #args=tuple([1,2,3,4,5,6,7,8# ])

def test1(x,*args):
print(x)
print(args)

test1(1,2,3,4,5,6)

## **kwargs:把N个关键字参数,转化成字典的方式
# def test2(**kwargs):
# print(kwargs)
# print(kwargs['name'])
# print(kwargs['age'])
# print(kwargs['sex'])
#
# test2(name="lzq",age = 8, sex = "b")
# #test2(**{'name':'lzq','age':8})

def test3(name,**kwargs):
print(name)
print(kwargs)

test3('lzq',age=1,sex='man')

def test4(name,age=18,**kwargs):
print(name)
print(age)
print(kwargs)

test4('lzq',sex='man',hobby='computer')

def test5(name,age=18,*args,**kwargs):
print(name)
print(age)
print(args)
print(kwargs)

test5('lzq',sex='man',hobby='computer')

#Author:walter

name1 = "walter" #全局变量
def change_name(name):
print("before change",name)
global name1 # 局部变量修改全局变量
name1 = "Walter"
name = "Lzq" #局部变量#这个函数就是这个变量的作用域,列表,字典,集合,类,可以通过局部修改全局变量
print("after changge",name)

name= "lzq"
change_name(name)
print(name,name1)

#递归
#Author:walter

def calc(n):
print(n)
return calc((n*2))
calc(1)

#高阶函数
def  add(a,b ,f):
return (f(a)+f(b)) #高阶函数
res = add(3,-6,abs)
print(res)

 

posted @ 2018-08-19 22:38  walter清  阅读(141)  评论(0编辑  收藏  举报