Coding and file operations

Character Encoding:

1: a text editor, file access principle

Save File: 1. First Start a text editor

        2 write contents are stored in memory is lost but now the amount of power failure powerful software can automatically save regularly

         3 ctrl + s to save data in memory will be permanently saved to the hard disk brush

 

Reading file:

       1 Start a text editor

       2 file contents from the hard disk into memory to

       3 text editor, the file contents of memory to print to the screen

 

2:

Character Encoding

Deposit with the binary-coded memory outputs 'Hello' is stored in memory, converted into a machine can be identified by a certain binary number

This conversion to follow certain rules of this law is called the character encoding

 

When there is the concept of character encoding

x=1

name = 'Hsiao Ming'

 

Running process:

1 Open pycharm interpreter to run into memory

2 to the memory to read the contents of the file (the data directly to the hard disk into memory)

3 explained in the corresponding binary concept python instruction to display characters but all will have a character encoding so the third step

There is the character encoding of gratitude

 

 

History of character encoding

Fourth represent two variations 00,011,011

Eight kinds of changes in 2 * 8256

An English word is represented by an 8-bit binary

An English = 8 bit = 1 Bytes

bit is a binary bit byte Bytes

This is called ASCII code

 

 

Later, when Chinese people use computers is too much man can not use the ASCII code

But this principle is still the Chinese added a 16 2 * 16 = 65536

China with GBK code

 

Is represented by a 1 Bytes represents a Chinese with English 2 Bytes

 

If you copy the Japanese software onto your computer's hard drive can not be saved, but not the same encoding format can not take

Unicode can not do the

So there is a compatible character encoding is the unicode encoded nations

 

####### memory character unicode will be used around the world to build relationships corresponding

2Bytes represents a character with special characters represented by more bytes

 

And then inverse solution adult classes in writing a word document in English, Chinese and Japanese regardless of the memory are converted into binary code unicode

字符出现在显示器上 。但如果要存储时 就保存在硬盘上 这是如果是英文的字符就会浪费一个字节

的空间

 

所以 就有了一个utf-8的编码  他是Unicode的升级 版  会自动中文 3个字节 英文一个字节

 

那么为什么内存不用utf-8 呢?因为unicode兼容以前的字符编码格式

 

3 保证不乱码

1内存中固定是unicode编码 唯一可以改变是存到硬盘时使用的编码

2要想不乱码应该保证文档当初以什么编码格式存的 就应该 以什么格式取出

 

 

python2 中

coding:utf-8

x='上'

print(x)

往空间放的值是'上'为unicode格式 而Python2 会把 x = '上'这个值转成头文件规定放在内存

如果不写头文件 会默认为  ASCII码  如果 x= 'sadasd'不出错 因为 utf-8兼容ASCII码

如果是x='上'中文出错

 

python3中

coding:utf-8

x='上'

print(x)

 

python3 会把这个值转成unicode码   不会有乱码

 

cmd 是windows提供的终端  在pycharm终端 不会乱码

 

python2 中这样解决

coding:utf-8

x=u'上'           # u 表示 unicode  意思是不用费劲了 就以unicode格式存吧 不乱码

print(x)           加小u 跨平台使用方便

 

python3 不加 因为默认是unicode

 

 

在python2中

coding:utf-8

x='上'

print(x)

print([x])  #  打印 3个字节的 Bytes  ['\xe4\xb8\x8a']   头文件改问gbk  就是二个字节

 

在python2 中有两种字符串类型

unicode

 x=u'上'

 

 unicode

 x= '上'    如果头文件是utf-8 就存成了utf-8 格式

 

python3  只有一种

unicode

x='上'

 

 

 

4 编码与解码
unicode转成其他编码 叫编码  encode**************
其他码 转成 unicode 叫解码  decode*********

python2 中
coding:gbk
x='上'
x.encode('utf-8')   #  这是  gbk 转unicode 所以是 解码
x.decode('utf-8')   # 可以解 但报错 因为存是gbk 解码也要 gbk
改为
x.decode('gbk')
res=x.decode('gbk')
print([res,])  # 内部是[,]表示看二进制数是多少


python 3  中没有解码  decode  只有编码encode 可以编码任意编码**********
x='上'
print(type(x))
res=x.encode('gbk')
print(res)    # 输出有一个b 前缀有一个b  表示 bytes 相当于 二进制
s=res.decode('gbk')
print(s)

总结:
在python3中的字符串类型 str 都是unicode 编码
在python2 中的字符串类型str 都是unicode按照文件头指定的编码
在python2中也可以制造unicode编码的字符串需要在字符串前加  u  跨平台不乱码
所以在python3中的字符串类型可以编码成其他任意字符编码的格式  结果是bytes 类型

# str  转   bytes
x='上'
b=x.encode('utf-8')
print(b)

# bytes  转  str
s=b.decode('utf-8')
print(s)
x='wwwww'
b=x.encode('utf-8')
print(b)
s=b.decode('utf-8')
print(s)

 

 

 

 

文件操作

绝对路径

f = open('d:\模特主妇护士班主任.txt',mode='r',encoding='UTF-8')

content = f.read()

print(content)

f.close()          # 关闭文件

 

 

 

f = open('模特主妇护士班主任',mode='rb',)   # b  是指读取内容为图片视频等不指定编码输出是一些二进制btyes

content = f.read()

print(content)

f.close()

 

 

 

对于w:没有此文件就会创建文件    只写w  前提是没有log'文件 没有创建文件  有先将源文件清空在写

f = open('log',mode='w',encoding='utf-8')

f.write('骑兵步兵')

f.close()

 

先将源文件的内容全部清除,在写。

f = open('log',mode='w',encoding='utf-8')

f.write('附近看到类似纠纷')

f.close()

 

 

 

 

f = open('log',mode='wb')

f.write('附近看到类似纠纷'.encode('utf-8'))   #附近看到类似纠纷 是写的文件内容  utf-8是指定的编码

f.close()

 

f = open('log',mode='a',encoding='utf-8')  #原理就是光标默认移到最后一位然后把内容追加到后面

f.write('佳琪')

f.close()

只能有一步操作 两步就报错所以有了'a+'模式

 

 

f = open('log',mode='a+',encoding='utf-8')

f.write('佳琪')

f.seek(0)

print(f.read())

f.close()

 

读写模式  r+

f = open('log', mode='r+', encoding='utf-8')

print(f.read())

f.write('大人,小孩')

print(f.read())

f.close()

 

由原来文件写的内容是python9 ,输出是pytho9 是因为写文件写哪光标移哪

看到就要用f.seek(0)调光标

 

写在前r+模式

f = open('log', mode='r+', encoding='utf-8')

f.write('你')

print(f.read())

f.close()

 

 

 

在文件中存的是python9结果输出hon9 前面3个字节被中文你覆盖掉了

写文件会从头开始写写完光标停在h 上后面就没有覆盖

 

写读文件w +

f=open('log',mode='w+',encoding='utf-8')

f.write('附近')

f.read()

f.close()

输出没有任何值 打开文件是附近,因为写的模式是没有创建有清空在写。不打印任何

想看可以调光标 f.seek()

 

功能详解

1

f = open('log',mode='r+',encoding='utf-8')

content = f.read(3)  # 读出来三个字符

print(content)

f.close()

# 输出  abd 三个字符  或  你好啊三个字符

# 小节:read(3)读出文件的最小单位  为一个字符中英文都是一个字符

 

 

2

 

f = open('log',mode='r+',encoding='utf-8')

# f.seek(3) # 调光标 调到三个字节从第四个字节开始读

# print(f.read())

# f.close()

# 文件中分别写进‘asdfgh’ 和‘附近的二哥’  分别读到  ‘fgh’  和‘近的二哥’

# 小节:utf-8一个中文三个字节  一个英文 一个字节

# seek(3)是安字节定光标

# read(3)是安字符定光标

 

3

 

f= open('log',mode='r+',encoding='utf-8')

f.seek(3)  # 是按照字节定光标的位置

f.tell() # 告诉你光标的位置 安字节

print(f.tell())

print(f.read())

f.close()

 

4

f = open('log',mode='r+',encoding='utf-8')

f.write('佳琪')# 读不出因为光标在最后

count=f.tell()#找光标的位置

f.seek(count-6)# 中文三个字节  两个中文

print(f.read())#读出佳琪

f.close()

 

其他功能

 

f.readable()  # 判断是否可读

line = f.readline()  # 一行一行的读

line = f.readlines()  # 每一行当成列表中的一个元素,添加到list中

f.truncate(4)

 

 

f= open('log',mode='r+',encoding='utf-8')

for line in f:

    print(line)

文件太大不能用

 

 

5

另一种写法 防止忘记关闭文件 f.close()

with open('log',mode='r+',encoding='utf-8') as f:

    f.read()

    print(f.read())

 

可以打开多个文件:

with open('log',mode='r+',encoding='utf-8') as f,\

        open('log',mode='w+',encoding='utf-8') as f1:

    print(f.read())

    print(f1.read())

 

# 注册登录

username = input('请输入你要注册的用户名:')

password = input('请输入你要注册的密码:')

with open('list_of_info',mode='w',encoding='utf-8') as f:

    f.write('{}\n{}'.format(username,password))

print('恭喜您,注册成功')

lis = []

i = 0

while i < 3:

    usn = input('请输入你的用户名:')

    pwd = input('请输入你的密码:')

    with open('list_of_info',mode='r+',encoding='utf-8') as f1:

        for line in f1:

            lis.append(line)

    if usn == lis[0].strip() and pwd == lis[1].strip():

        print('登录成功')

        break

    else:print('账号和密码错误')

    i+=1

Guess you like

Origin www.cnblogs.com/andy117/p/11005612.html