yue_427

导航

字符串、集合、文件操作

name = "my name is Alex"
print(name.capitalize())#首字母大写
print(name.count("a"))#统计字母个数
print(name.center(56,"-"))#
print(name.encode())#字符串转成二进制
print(name.endswith("ex"))#以什么结尾
print(name.expandtabs(tabsize=30))#写一个tab键,把tab键转成多少个空格
print(name[name.find("name")])#找到字符的开头,字符串切片
print(name.format(name='alex',year=23))
print('ab123'.isalnum())#阿拉伯数字
print('1a'.isalpha())#可以纯英文字符
print('1a'.isdigit())#整数
print('a 1A'.isidentifier())#判断是不是一个合法的标识符
print('33'.isnumeric())#判断是不是数字
print('My Name Is'.istitle())#首字母大写
print('My Name Is'.isupper())#全是大写
print('+'.join(['1','2','3']))#输出结果1+2+3
print(name.ljust(50,'*'))#后面补全
print(name.rjust(50,'-'))#前面补全
print('Alex'.lower())#把大写换小写
print('Alex'.upper())#把小写换大写
print('Alex\n'.lstrip())#去左空格回车
print('Alex\n'.rstrip())#去右空格回车
print('Alex\n'.strip())#去空格回车

p= str.maketrans("abcdef",'123456')#数字对应给字母(写随机密码)
print("alex li".translate(p))


print('alex'.replace('l','L'))#替换
print('alex lil'.rfind('l'))#找到最右边的值的下标
print('1+2+3+4'.split('+'))#提取
print('1+2\n+3+4'.splitlines())#换行
print('Alex Li'.swapcase())#大写换小写,小写换大写
print('lex li'.title())
print('lex li'.zfill(50))#不够用零填充


集合操作:
可以去重,关系测试
集合也是无序的
list_1 =[1,4,5,7,3,6,7,9]
list_1 = set(list_1)
print(list_1,type(list_1))

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

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

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

#差集
print(list_1.difference(list_2))

#子集
list_3 = set([1,3,7])
print(list_3.issubset(list_1))
print(list_1.issuperset(list_3))

#反向差集
print(list_1.symmetric_difference(list_2))


print(list_1 & list_2)#交集
print(list_1 | list_2)#并集
print(list_1 - list_2)#差集
print(list_1 ^ list_2)#对称差集
print(list_1 # list_2)#子集
 
list_1.add(999) #添加
list_1.update([888,777,555]) #添加多项

print(list_1.remove(888))#指定一个值,如果没有会报错
print(list_1.pop())#删除

list_1.discard(888)#删除


文件操作:
读:
f = open("yes",'r',encoding="utf-8") #文件句柄
data = f.read()
print(data)
f.close

写:
f = open("yes",'w',encoding="utf-8")  #文件句柄
f.write("花园的花朵真鲜艳")
f.close


追加:#a=append
f = open("yes",'a',encoding="utf-8")  #文件句柄
f.write("花园的花朵真鲜艳")
f.close

#怎么读前五行
f = open("yes",'r',encoding="utf-8")
for i in range(5):
  print(f.readline())

#读10行,第11行不打印
count = 0
for i in f:
if count == 9
print('-----')
count += 1
continue
print(i)
count += 1






teel and seek的用法:
f = open("yes",'r',encoding="utf-8")  #文件句柄
print(f.tell()) #tell按照字符计数
print(f.readline())
print(f.readline())
print(f.readline())
print(f.tell())#打印当前的位置
f.seek(10)#seek回到哪个地方
print(f.readline())
f.close
 
f = open("yes",'r',encoding="utf-8")  #文件句柄
print(f.tell()) #tell按照字符计数
print(f.readline())#打印一行
print(f.readline())#打印一行
print(f.readline())#打印一行
print(f.tell())#打印当前的位置
f.seek(10)#seek回到哪个地方
print(f.readline())


f.flush()#实时刷新
import sys,time
for i in range(100):
sys.stdout.write("#")
sys.stdout.flush()
time.sleep(0.1)
#截断
f = open("yes",'a',encoding="utf-8")  #文件句柄
f.truncate(10)



#修改:
f = open("yes",'r+',encoding="utf-8")  #文件句柄 读写
f = open("yes",'w+',encoding="utf-8") #文件句柄 写读(基本上没有什么用)
f = open("yes",'a+',encoding="utf-8") #文件句柄 追加读
f = open("yes",'rb') #文件句柄 二进制文件(用于网络传输)
f = open("yes",'wb')
f.write("hello binary\n".encode)

print(f.readline())
print(f.readline())
print(f.readline())
print(f.tell())
f.write("------------")
print(f.readline())






#修改:
f = open("yes","r",encoding="utf-8")
f_new = open("yes2.bak","w",encoding="utf-8")
for line in f:
if "肆意的快乐等我享受" in line:
line = line.replace("肆意的快乐等我享受","肆意的快乐等yang享受")
f_new.write(line)
f.close()
f_new.close()







posted on 2017-03-20 14:50  yue_427  阅读(161)  评论(0编辑  收藏  举报