day08 python
A. File Operations
1. The file operation function
open (filename, mode = mode, encoding = character set)
2.模式: r, w, a, r+, w+, a+, rb, wb, ab, r+b, w+b, a+b
r: read()
f = open('num.py', mode='r', encoding='utf-8')
print (f.read (3)) # read on behalf of three characters, English is a single character, a single Chinese character is a character
print (f.read ()) # read, all, large files can not, taking into account memory
f.close()
r: readline()
f = open('num.py', mode='r', encoding='utf-8')
print (f.readline (), end = '\ n') # a read line, the cursor will move
print (f.readline (), end = '\ n') #print () function has a line feed, and the readline () will read the text out of line breaks
print (f.readline (). strip (), end = '\ n') # remove a line feed, remove the readline (), and blank lines in the original print out
print (f.readline (), end = '') # remove a line feed, remove print () of
f.close()
r: while
f = open('num.py', mode='r', encoding='utf-8')
while True:
s = f.readline()
if s != '':
print(s,end='')
f.close () # parked in this program, have been waiting (when the file has increased, will continue to print), to do with monitoring
print('program is over')
r: for
f = open('num.py', mode='r', encoding='utf-8')
for line in f:
print (line, end = '') # and raadline () as read line by line, text will wrap read out
r: coding
f = open (r'C: \ Users \ THINKPAD \ Desktop \ bajie.txt ', mode =' r ', encoding =' gbk ') # When reading a file window (default Chinese gbk)
print (f.read ()) # 1. window file paths r ''
f.close () # 2. When there is Chinese, what encoding, it does not matter what no Chinese decoded #
w: write()
f = open ( 'num.py', mode = 'w', encoding = 'utf-8') # w, with (w, w +, w + b), as long as you operate, it will empty the first source file
f.write ( 'bajieaishuishui \ n') # there are functions to create the file: When the file does not exist, create
f.write('bajieaishuishui\n') #一行一行写的时候, 和readline()一样, 要注意换行符的问题
f.flush()
f.close()
a: write()
f = open('num.py', mode='a', encoding='utf-8')
f.write('bajieaishuishui\n')
f.close()
rb, wb, ab: b要和模式配合(b的意思是bytes), 处理的是非文本文件
f = open(r'C:\Users\THINKPAD\Desktop\tupian.jpg', mode='rb') #处理的是二进制,用不了 encoding
e = open(r'C:\Users\THINKPAD\Desktop\tupiancp.jpg', mode='wb')
for line in f:
print(len(line)) #一次读一行, 读了多少不固定, 不用管
e.write(line)
f.close()
e.close()
r+: 先读后写
r+ 依旧是最好用的读写同时存在的模式
f = open('num.py', mode='r+', encoding='utf-8') #读写的顺序,
s = f.readline() #r+ 模式(唯一和光标原则不相符的地方), 当你先读后写时, 不论你读了多少,写入时都在文件最后写
f.seek(3) #当然你可以强行改变光标的位置
f.write('八戒\n')
print(s)
f.close()
r+: 先写后读
f = open('num.py', mode='r+', encoding='utf-8')
f.write('八戒')
s = f.read() #先写后读里面貌似也有问题, 先写的写到哪?
print(s) #按理说是写到开头, 但是你后面跟的读如果不是read()读全部, 那么他也是写到最后
f.close()
w+: 基本不用, 因为遇到w就清空特性不好
f = open('num.py', mode='w+', encoding='utf-8')
f.write('八戒') #写完之后, 光标在最后, 再读是没内容的
f.seek(0) #光标移到开头
s = f.read()
print(s)
f.close()
a+: 不常用, 因为开始的时候光标就在末尾,麻烦
f = open('num.py', mode='a+', encoding='utf-8')
f.seek(0)
print(f.read())
f.close()
r+b, w+b, a+b: 一个二进制的文件不太可能同时读写, 很少用
3.常用的操作
光标 seek()
f = open('num.py', mode='r', encoding='utf-8')
f.seek(0) #这里的数字代表的是字节 bytes, utf-8 的汉字要用 3 个
f.seek(0, 2)
print(f.read(1)) #这里的数字代表的是字符
f.close()
#f.seek(offset,whence) # offset偏移量,用的是bytes 为单位; 正数:向前冲; 负数:没有 # whence 0:从开头 1:从当前 2:从末尾
tell()
f = open('num.py', mode='r', encoding='utf-8')
f.seek(3)
print(f.tell()) #和f.seek()一样, 告诉你在哪个bytes 的位置
f.close()
truncate() 截断
f = open('num.py', mode='w', encoding='utf-8')
f.write('八戒爱谁谁')
f.seek(6)
f.truncate() #默认把光标之后的内容干掉; 如果给了参数 n , 则干掉 n 后面的
f.close()
二.文件的修改(比如字符串替换, 不支持直接改)
import os
with open('num.py', mode='r', encoding='utf-8') as f1, open('num.py.fuben', mode='w', encoding='utf-8') as f2:
for line in f1:
line = line.replace('爱','----*----')
f2.write(line) #把改好的每行写到副本文件中
os.remove('num.py') #把原来的文件删掉
os.rename('num.py.fuben','num.py') #把副本改名成原来的文件
三.简单日志处理
日志格式及需求
1,八戒, 10086, sioon #每一行搞成这样 {'id':'1','name':'bajie','phone':'10086','car':'sioon'}, 最后搞到一个列表里
2,悟空, 36600, stang
3,信息, 45890, itngl
代码
ls = []
with open('num.py',mode='r',encoding='utf-8') as f:
for line in f:
dic = {}
lst = line.strip().split(',')
dic['id'] = lst[0]
dic['name'] = lst[1]
dic['phone'] = lst[2]
dic['car'] = lst[3]
ls.append(dic)
print(ls)
练习:
求水仙花数
for num in range(100,1000):
sum3 = 0
num = str(num)
for i in num:
sum3 = sum3 + int(i)**3
if sum3 == int(num):
print('%s yes' % num)
random模块
import random
print(random.randint(0,1)) #取随机数字, 可以猜到结尾
搞7个随机数, 不重复
import random
s = set() #利用集合的不重复的特性
while len(s) < 7:
s.add(random.randint(1, 36))
print(s)
两个变量的数据交换
a = 10
b = 5
a, b = b, a
print(a,b)