文字コードとファイル処理



cpu



: py , ,

py python

python python
python3 test.py
: python
: test.py
: ,


----》 -------》
:


ASCII
8bit=1bytes, 1

GBK
16bit=2bytes ,2 ,1

unicode( ):
16bit=2bytes,2

utf-8:Unicode Transformation Format
1 ,3bytes








python3 : utf-8
python2 : ascii

:#coding:utf-8 python ,

: python
python ,
x=' ' #x=str(' ')
python2:

x=' ' #python2 str ‘ ’

python3:
str: unicode



unicode--- encode--->gbk
gbk-------> decode---》unicode

print(2**16-1)

: , , !




1、    
f=open(r' ',mode=' ',encoding=' ')
f=open(r'a.txt',mode='r')
print(f)
#2、 /
data=f.read()
print(data)

#3、 ( )
f.close() #
# print(f)
# f.read()
# del f
# x=1


          :r,w,a
          t  :    ,        

#r: ,
# : ,

#w:
#
# 1、 ,
# 2、 ,

# f=open(r'a1.txt',mode='w',encoding='utf-8') # wt
# # f.write('
')
# # f.write('
')
#


#a: ( )
# :
# ,
# ,


    
f.read() #
f.readline()
f.readlines()


f.write('1111111
222222
') # ,
f.writelines(['1111
','2222
']) #


for line in f:
print(line)

転載先:https://www.cnblogs.com/liqingxiaobai/articles/8624914.html