20200110--python学习第八天
今日内容
进制
对于计算机而言无论是计算机存储或是网络传输输入的本质都是:二进制;例如电脑上存储的视频/图形/文件/微信/qq的表情包/小视频都是二进制。
二进制:计算机内部
八进制:
十进制:
十六进制:一般用于表现二进制(就是用更短的内容表示更多的数据),一般都是:\x开头
进制对应关系

字符串
(1)判断一个字符串是否是整型,一般使用isdecimal()
应用示例:
v1 = '1'
v2 = '二'
v3 = '②'
print(v1.isdigit() ,v2.isdigit(),v3.isdigit())
# '1'-> True; '二'-> False; '②' --> True
print(v1.isdecimal(),v2.isdecimal(),v3.isdecimal())
# '1'-> True; '二'-> False; '②' --> False
print(v1.isnumeric(),v2.isnumeric(),v3.isnumeric())
# '1'-> True; '二'-> True; '②' --> True print(v1,v2,v3)
# 以后推荐用 isdecimal 判断是否是 10进制的数
#############应用
v = ['alex','eric','tony']
for i in v:
print(i)
num = input('请输入序号:')
if num.isdecimal():
num = int(num)
print(v[num])
else:
print('你输入的不是数字')
(2)strip()函数可以去掉空白,\t制表符即tab键,\n换行
(3)center/ljust/rjust/count/zfill等其他方式
文件打开模式
(1) r/w/a :read读取文件内容时按照字符串
(2)r+/w+/a+ :read读取文件内容时按照字符串
(3) rb/wb/ab :read读取文件内容时按照字节数
(4) r+b/w+b/a+b :read读取文件内容时按照字节数
示例:
#read() 读取全部文件内容
#read(1) 读取字符
file_object = open(file ="a.txt",mode = "r",encoding= "utf-8")
data= file_object.read(1) #表示一个字符
file_object.close()
print(data)
#read(1) 读取字节
file_object = open(file ="a.txt",mode = "rb")
data= file_object.read(3) #表示3个字节,一个字符
file_object.close()
print(data.decode("utf-8"))
#write(字符串)
file_object = open(file ="aa.txt",mode = "w",encoding= "utf-8")
data= file_object.write("中国你好") #表示写入字符串
file_object.close()
#write(二进制)
file_object = open(file ="aa.txt",mode = "wb")
data= file_object.write("中国你好,我爱你我的家".encode("utf-8")) #表示写入二进制
file_object.close()
#seek(移动光标字节数) 无论处于哪种模式下,都是按照字节移动的
file_object = open(file ="aa.txt",mode = "r",encoding= "utf-8")
file_object.seek(6) #移动光标到两个字符
data= file_object.read() #读取光标后边的所有内容
file_object.close()
print(data)
#tell 获取光标当前所在的字符位置
file_object = open(file ="aa.txt",mode = "r",encoding= "utf-8")
file_object.seek(6) #移动光标到两个字符
#file_object.read() #读取光标后边的所有内容
curren_index= file_object.tell()
file_object.close()
print(curren_index)
# flush 强制将内存中的数据刷到硬盘上
file_object = open(file ="aa.txt",mode = "a",encoding= "utf-8")
while True:
user_name = input("请输入你的账户名:")
file_object.write(user_name+'\n')
file_object.flush()
file_object.close()
关闭文件
#文艺青年
file_object = open(file="aa.txt",mode="w", encoding = "utf-8")
file_object.close()
#二逼
with open(file="aa.txt",mode="w", encoding = "utf-8") as file_object:
file_object.write("中国好青年")
#缩进中的代码执行完毕后,自动关闭文件
文件修改
#替换文件中的内容,然后生成一个新文件
file_object = open(file="a.txt",mode="r", encoding = "utf-8")
data = file_object.read()
new_data = data.replace("老师","教书育人,伟大的事业,一生奋斗")
file_object2 = open(file="c.txt",mode="w", encoding = "utf-8")
file_object2.write(new_data)
file_object.close()
file_object2.close()
#大文件修改
file_object = open(file="a.txt",mode="r", encoding = "utf-8")
file_object2 = open(file="c.txt",mode="w", encoding = "utf-8")
for line in file_object:
new_data = line.replace("老师","教书育人,伟大的事业,一生奋斗")
file_object2.write(new_data)
file_object.close()
file_object2.close()
#方法二
with open(file="a.txt",mode="r", encoding = "utf-8") as f1, open(file="c.txt",mode="w", encoding = "utf-8") as f2:
for line in f1:
new_data = line.replace("老师", "教书育人,伟大的事业,一生奋斗")
f2.write(new_data)
文件的收集

浙公网安备 33010602011771号