字符编码 文件处理

# 字符编码
# unicode-->encode-->utf-8 编码
# utf-8-->decode-->unicode 解码

# 存取文件不乱码的关键:用什么编码存的,就要用什么编码读

# python3默认的解释器是utf-8
# python2默认的解释器是ascii

# 通过某种方式告诉它使用哪一种字符编码打开文件,在文件的开头来一个#coding:utf-8

# x='上' # 在python3中字符串默认就是unicode
# b=x.encode('utf-8')
# print(type(b.decode('utf-8'))) # <class 'str'>

# print(x.encode('gbk')) # b'xc9xcf'
# print(x.encode('utf-8')) # b'xe4xb8x8a'

# python2的unicode字符串就是python3的str类型
# python2的str类型就是python3的bites类型

# 文件处理
# 读操作 # 只读模式 默认是rt文本读
# f=open('a.txt')
# f=open('D: exta.txt'') # 绝对路径 可能有特殊意义,可以添加r
# f=open(r'D: exta.txt') # 原生字符串,就是里面的没有特殊意义

# f=open('a.txt','r') # 默认打开模式为r
# date=f.read()
# print(date)
# # f.close() # 报错,windows默认的操作系统是gbk,而保存的文件用的是utf-8

# f=open('a.txt','r',encoding='utf-8') #
# date=f.read()
# print(date)
# f.close() # 文件关闭,回收的是操作系统的资源
# # print(f) # OBOS Tank hello world Sean Egon age 18
# f.read() # 报错

# with
# with open('a.txt','r',encoding='utf-8') as f: # 打开文件赋值给f
# pass # 后面代码结束后with会自动执行文件关闭功能

# f=open('a.txt','r',encoding='utf-8')
# date1=f.read()
# print(date1) # OBOS Tank hello world Sean Egon age 18
# date2=f.read()
# print(date2) # 第一次读取有结果,第二次没有。第一次读取时,光标从文件开头读到结尾,第二次读取时,光标还在结尾

# f=open('a.txt','r',encoding='utf-8')
# print(f.readline()) # 每次只读取一行 # OBOS
# print(f.readline()) # Tank
# print(f.readline()) # hello world # print打印自带换行功能
# print(f.readline(),end='') # Sean
# print(f.readline(),end='') # Egon
# f.close()

# f=open('a.txt','r',encoding='utf-8')
# print(f.readlines()) # 把结果读出来放在一个列表
# f.close() # ['OBOS ', 'Tank ', 'hello world ', 'Sean ', 'Egon ', 'age ', '18']
#
# read 和 readlines适用于较小的文件,readline每次只读一行,不局限于文件大小

# 写操作 只写模式 默认是wt文本写 只能是字符串模式
# 如果文件不存在,则创建,存在,则覆盖/清空
# f=open('a.txt','w',encoding='utf-8') # 默认文集为t模式
# f.write('1 ')
# f.write('22 ')
# f.write('1 22 333 ')
# f.writelines(['1 ','3','5'])
# f.write(8888) # 报错
# f.close()

# a 文件不存在则创建,存在则从文件末尾开始写
# f=open(r'b.txt','a',encoding='utf-8')
# f.write('1 ')
# f=open('b.txt','a',encoding='utf-8')
# f.write('100 ')
# f.writelines(['1 ','10 ','1000 '])
原文地址:https://www.cnblogs.com/0B0S/p/11938372.html