Character encoding review and file basis (2)

1. Character encoding
Correspondence table of characters and numbers
ASCII: can recognize English characters, 1Byte=1 English characters
hello
8bit|8bit|8bit|8bit| 8bit

GBK:汉字与英文字符,2Byte=1中文字符1Byte=1英文字符
你a好
8bit|8bit|8bit|8bit|8bit

1111 1111|1111 1111 首位用来标识中文字符的一部分

0111 1111|0111 1111 英文字符首位是0
在内存编码中保存的是去掉首位进行保存的

< br> Unicode
1. Compatible with universal characters
2. It has a mapping relationship with other binary numbers of any encoding
Unicode->GBK encoding
GBK->Unicode decoding
UTF-8 :是Unicode的精简版(转换格式)3个Bytes=1中文字符,1Byte=1英文字符

2.在python中字符编码的应用
1.保证执行python程序的前两Stage not garbled, plus the file head
Write the first line of the file: #coding: Use the code when the file is stored
2. The evolution of the string type:
Python2 has two “字符串”类型:
类型一
#coding:gbk
x=’上’ #’上’存成了GBK编码侯的二进制

类型二
x =u’上’ #’上’存成了Unicode类型

python3有两种“字符串”相关类型
x=’上’#上存成了Unicode编码侯的二进制

x.encode(‘gbk’)转换为二进制编码是byte类型
字符串可以编码成二进制类型

3.总结:
1.保证不乱码:以什么编码存的就应该以什么编码取
2.在python2中定义字符串,应该加上前缀u
3.在编写python文件时必须加上文件头

文件处理的Basic process:
1. Open the file and get the file object (file object ====> file opened by the operating system ==> hard disk)
f=open(r’file path’,mode=’file Open mode’, encoding=’character encoding’) r代表原生字符串

2.操作文件:读/写
f.read()
f.readlines()
f.readline()
f.readable( )

3.向操作系统发送指令关闭文件,回收操作系统资源
f.close()
f=open(r’文件位置’,mode=’rt’ ,encoding=’utf-8′)
f.close()
print(f)

二:上下文管理
with open(r’文件位置’,mode =’rt’,encoding=’utf-8′) as f:
data=f.read()
print(data)
print(‘=’*100)
for line in f:#读不出内容第一步已经把文件内容读完文件指针已经指向到最后了
print(line)

今日内容
1.文件打开模式
2 .文件操作方法
3.文件内指针移动(不是主动控制的是被动触发的)

打开文件的模式有三种纯净模式:r(默认的) wa
控制操作文件Two modes of content: t (default) b
Major premise: tb mode cannot be used alone, it must be used in combination with pure mode
t text mode:
1. Read and write files are all in characters String as a unit
2. Only for text files
3. Encoding parameter must be specified
b Binary mode:
1. Reading and writing files are all based on Bytes/Binary
2. Can be for all files
3. Encoding parameters must not be specified

Detailed explanation of the second open file mode
1.r Read-only mode: an error is reported when the file does not exist, and the file exists in the file The pointer goes directly to the beginning
with open(‘file location’,mode=’rt’,encoding=’utf-8′)
print(f.readlines())


2.w Write-only mode: create an empty document when the file does not exist, the file pointer will go to the beginning of the file if the file exists
with open(‘b.txt’,mode=’wt’,encoding=’utf -8′) as f:
print(f.writable())#Writable return true writable< br> print(f.readable())#If it is readable, return false and not readable
f.write(‘Hello
‘)
f.write(‘我好
‘)#Emphasis: in If the file is not closed and cleared, the content written later must follow the content written first
f.write(‘Hello everyone
‘)
f.write(‘111
22222
3333
‘)< br> lines=[‘1111′,’2222′,’33333’]
for line in lines:
f.write(lines)
f.writelines(line)

#User authentication function
inp_name=input(‘Please enter your name:’).strip()
inp_pwd=input(‘Please enter your password:’).strip()
with open( r’.txt’,mode=’rt’,encoding=’utf-8′) as f:
for line in f:#Compare the name and password entered by the user with the read content
u ,p=line.strip(‘
‘).split(‘:’)
if inp_name==u and inp_pwd==p:
print(‘Login successful’)
break
else:
print(‘账号和密码错误’)

#注册功能:
name=input(‘username>>>:’).strip()
pwd=input( ‘password>>>:’).strip()
with open(r’.txt’,mode=’at’,encoding=’utf-8′) as f:
info=’%s: %s
‘%(name,pwd)
f.write(info)
3.a Only append write mode: when the file does not exist, an empty document will be created, and the file will be moved directly to the The end of the file
with open(r’.txt’,mode=’at’,encoding=’utf-8′) as f:
f.write(‘4444
555
‘)

#r+ w+ a+
with open(r’a.txt’,mode=’r+t’,encoding=’utf-8′) as f:#r+t read and write mode
print(fr eadline())
f.write(‘Hello’)

b: Reading and writing are in binary units
with open(r’.txt’,mode=’rb’ ) as f:
print(f.readline())
f.write(‘你好’)
with open(r’.txt’,mode=’rb’) as f:
print(f.readline())
data = f.read()
print(data.decode(‘utf-8’))#Using utf-8 for decoding
with open(r’ 1.png’,mode=’rb’) as f:
print(f.readline())
data = f.read()
print(data)#Cannot decode, only output binary< /p>

with open(r’.txt’,mode=’wb’) as f:
f.write(‘你好’.encode(‘utf-8’))
copy and paste The principle of
with open(r’1.png’,mode=’rb’) as f:
data=f.read()

with open(r’2.png’ ,mode=’wb’) as f: f.write(data)# Copy tool src_file=input(‘source file path:’).strip()dst_file=input(‘target file path:’).strip()with open(r’%s’ %src_file,mode=’rb’) as read_f,open(r’%s’ %dst_file,mode=’wb’) as write_f: for line in read_f: # print(line) write_f. write(line)

Leave a Comment

Your email address will not be published.