23 Mar 18 文件處理

複習:python

字符編碼表(字符和數字一一對應關係的表)需掌握:linux

 一、以什麼編碼存的,就應該以該編碼取
        #coding:utf-8 :用於python解釋器讀取python文件,
         因此文件頭指定的編碼必須跟python文件存儲時用的編碼一致

 二、 編碼解碼
        unicode---編碼encode---》utf-8
        utf-8---解碼decode---》unicode
        python3中str是以unicode編碼形式存放的
        x='你好'
        x.encode('utf-8') ----> bytes
        bytes.decode('utf-8')------------->str(unicode)

 三、bytes類型(可當成二進制的數據, bytes爲python3中新的數據類型)的用途:
     一、存放到文件中(bytes類型能夠直接扔給硬盤)
     二、基於網絡傳輸windows

  ## x.encode(‘utf-8’)的結果是一個放在內存中的值網絡

 

文件處理:
    with open('a.txt', encoding='utf-8') as f1,\
            open('b.txt', encoding='utf-8') as f2,\
            open('c.txt', encoding='utf-8') as f3:
        pass
## \+回車 在pycharm中換到下一行,但實際上仍是一行

    默認是t模式,t不能單獨使用,必須是rt,wt,at編碼

 

f.read() 讀出來的是一個打的字符串
f.readlines() 至關於一個for循環
 
絕對路徑與相對路徑:
絕對路徑:在任意位置均可以定位該文件
相對路徑:以所在位置爲參考定位文件
 
…\py_learn\day1\test.py: windows 系統用‘\’表示子關係
…/py_learn/day1/test.py: linux系統用‘/ 表示子關係’

 

while True:
    msg="""
    1
註冊
    2 查看
    """
   
print(msg)
    choice=input('輸入編號>>: ').strip()
    if choice == "1":
        print('\033[45m註冊。。。\033[0m')
        phone=input('手機號>>: ').strip()
        name=input('用戶名>>: ').strip()
        pwd=input('密碼>>: ').strip()
        sex=input('性別>>: ').strip()
        with open('db.txt','at',encoding='utf-8') as f:
            f.write('%s,%s,%s,%s\n' %(phone,name,pwd,sex))
    elif choice == '2':
        print('\033[44m查看。。。。。\033[0m')

        phone=input('請輸入你的手機號:').strip()
        with open('db.txt','rt',encoding='utf-8') as f:
            for line in f:
                if line.startswith(phone):
                    print(line,end='')
                    break
    else
:
        print('\033[43m輸入非法編號...\033[0m']

 

文件處理:命令行

文件的打開模式b模式
強調:
一、與t模式相似不能單獨使用,必須是rb,wb,ab
二、b模式下讀寫都是以bytes單位的
三、b模式下必定不能指定encoding參數

 

rb模式code

with open('1.jpg',mode='rb',) as f:
    data=f.read()
    print(data,)
    print(type(data))

with open('db.txt',mode='rb',) as f:
    data=f.read() #
   
print(data.decode('utf-8')) #bytes-----unicode
   
print(type(data))

#‘utf-8’等是字符編碼,只能處理字符,處理不了包括圖片視頻在內的其餘形式視頻

# b模式(二進制)也能夠讀txt,但要讀出字符,須要解碼圖片

 

wb模式ip

with open('b.txt',mode='wb') as f:
    msg='你好啊,吳三炮'
   
f.write(msg.encode('gbk'))   #在utf-8平臺亂碼,reload去gbk可讀
with open('b.txt',mode='ab') as f:
    msg='你好啊,吳三炮'
   
f.write(msg.encode('utf-8'))  #在gbk平臺亂碼,reload去utf-8可讀

 

with open('b.txt',mode='rb') as f:
    data=f.read()
    print(type(data))
    print(data.decode('utf-8'))

with open('1.jpg',mode='rb') as f:
    data=f.read()
    print(type(data))
    print(data.decode('utf-8'))

##error, utf-8不能處理非字符類型文件

 

ab模式

with open('b.txt',mode='ab') as f:
    f.write('你好'.encode('utf-8'))

with open('1.jpg','rb') as f:
    for line in f:
        print(line)

 

可讀可寫 #不改變r/w/a在判斷文件存在與否後做出的基本處理方法, 不多用
#r+t (r+)
with open('b.txt','r+t',encoding='utf-8') as f:
    print(f.readable())
    print(f.writable())
    print(f.readline())
    f.write('\n吳大炮你也號\n')
#w+t (w+)
#a+t (a+)
#U   :通用換行符,沒用

 

文件的處理

with open('db.txt','r+',encoding='utf-8') as f:
f.seek(9) #偏移量的單位是字節,utf-8中,一箇中文字符佔三個字節,一個英文字符佔一個字節
print(f.tell())   #9
print(f.read())
 
硬盤無刪改,只是覆蓋。刪除後,硬件上的原空間被標定free。能夠在內存中刪改
修改文件方式一:
一、先把文件內容所有讀入內存
二、而後在內存中完成修改
三、再把修改後的結果覆蓋寫入原文件
缺點:會在文件內容過大的狀況下,佔用過多的內存

with open('user.txt',mode='r',encoding='utf-8') as f:
    data=f.read()
    data=data.replace('吳佩其','吳佩其[老男孩第二帥的人]')
with open('user.txt',mode='w',encoding='utf-8') as f:
    f.write(data)

修改文件方式二:
一、以讀的方式打開原文件,以寫的方式打開一個新文件
二、讀一行原文內容,寫入新文件,若是該行內容是須要修改的內容,那麼修改完後再寫入新文件
三、刪除原文件,將新文件名重命名爲原文件名

import os
with open('user.txt',mode='rt',encoding='utf-8') as read_f,\
        open('user.txt.swap',mode='wt',encoding='utf-8') as write_f:
    for line in read_f:
        if '吳佩其' in line:
            line=line.replace('吳佩其','吳佩其[老男漢特別特別的老]')
        write_f.write(line)
os.remove('user.txt')
os.rename('user.txt.swap','user.txt')

#總的來講第一種方式耗內存,第二種方式耗硬件

 

copy #在終端中運行

import sys
l=sys.argv # 把命令行中解釋器後空格分割的全部參數都存成列表
# print(l)

src_file_path=l[1]
dst_file_path=l[2]
# print(src_file_path)
# print(dst_file_path)
with open(r'%s' %src_file_path,mode='rb') as src_f,\
        open(r'%s' %dst_file_path,mode='wb') as dst_f:
    for line in src_f:         dst_f.write(line)
相關文章
相關標籤/搜索