编码
二进制
计算机算法二进制:0和1,所有数据都用01表示
000 0
001 1
010 2
011 3
100 4
101 5
110 6
111 7
举例:网络宽带百兆,下载速度十几兆
编码表
二进制
四进制
8进制
16进制

python默认使用编码是unicode,可输入任何语言
数据储存和网络传输编码是UTF-8
encode()和decode()
内存处理:unicode
储存、传输:utf-8
#'编码内容'.encode('编码表')
#'解码内容'.decode('编码表')
print('吴枫'.encode('utf-8'))
print('吴枫'.encode('gbk'))
print(b'\xe5\x90\xb4\xe6\x9e\xab'.decode('utf-8'))
print(b'\xce\xe2\xb7\xe3'.decode('gbk'))
文件读写
读取文件
#绝对路径,是最完整的路径
#相对路径,是当前文件夹的位置

#读文件:开——读——开
#打开文件:open()函数赋值变量,第一参数为路径,第二参数为模式,第三参数为编码类型
file1 = open('/Users/zhanglihui/Desktop/test/abc.txt','r',encoding='utf-8')
filecontent = file1.read()
print(filecontent)
file1.close()
#关闭文件:变量.close()
#第二参数:'r'=read缩写 'w'=write缩写
写入文件
#读文件:开——读——开
#打开文件:open()函数赋值变量,第一参数为路径,第二参数为模式,第三参数为编码类型
file1 = open('/Users/zhanglihui/Desktop/test/abc.txt','a',encoding='utf-8')
file1.write('张无忌\n')
file1.write('宋青书\n')
file1.close()
#关闭文件:变量.close()
#第二参数:'w'=write缩写 暴力清空原文件重新编写 'a' 在原基础上添加
#'w'创建1个文本,并填写元素
file1 = open('/Users/zhanglihui/Desktop/test/1.txt','w',encoding='utf-8')
file1.write('难念的经\n')
#'r'打开文本
file1 = open('/Users/zhanglihui/Desktop/test/1.txt','r',encoding='utf-8')
aaa = file1.read()
print(aaa)
file1.close()

#'w'创建1个文本,并填写元素
with open('/Users/zhanglihui/Desktop/test/1.txt','w') as file1:
file1.write('难念的经\n')
#'r'打开文本
with open('/Users/zhanglihui/Desktop/test/1.txt','r') as file1:
aaa = file1.read()
print(aaa)
#为防止不关闭文件造成资源浪费,with open……as 变量名
网友评论