您好,登錄后才能下訂單哦!
本文小編為大家詳細(xì)介紹“Python怎么實(shí)現(xiàn)文件操作幫助類”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“Python怎么實(shí)現(xiàn)文件操作幫助類”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來(lái)學(xué)習(xí)新知識(shí)吧。
在使用Python進(jìn)行業(yè)務(wù)開(kāi)發(fā)的時(shí)候,需要將一些數(shù)據(jù)保存到本地文件存儲(chǔ),方便后面進(jìn)行數(shù)據(jù)分析展示。
通過(guò)查看需求可得出:需要將數(shù)據(jù)存儲(chǔ)為本地文件(這就是涉及到文件的操作),文件操作屬于基礎(chǔ)內(nèi)容,可以直接將常用的文件操作封裝為一個(gè)文件,后面使用直接調(diào)用即可。
#文件操作 import pickle #讀取文件的所有內(nèi)容(返回字符串) def ReadFileAllInfoAsStr(filePathAndName): try: with open(filePathAndName) as fileObj: fileInfos=fileObj.read() except FileNotFoundError: msg="很抱歉,文件【"+filePathAndName+"】不存在" print(msg) else: return fileInfos #讀取文件的所有內(nèi)容(返回列表) def ReadFileAllInfoAsList(filePathAndName): try: with open(filePathAndName) as fileObj: fileInfos=fileObj.readlines() except FileNotFoundError: msg="很抱歉,文件【"+filePathAndName+"】不存在" print(msg) else: return fileInfos #寫(xiě)入信息到文件(覆蓋原有內(nèi)容) def WriteInfo(needWriteInfo,filePathAndName): try: with open(filePathAndName,'wb') as fileObj: tmpBytes = bytes(needWriteInfo,'utf8') fileObj.write(tmpBytes) except Exception as e: print(e) #追加信息到文件中 def AppedInfo(needWriteInfo,filePathAndName): try: with open(filePathAndName,'ab') as fileObj: tmpBytes = bytes('\n'+needWriteInfo,'utf8') fileObj.write(tmpBytes) except Exception as e: print(e) #寫(xiě)入對(duì)象到文件 def WriteObj(needWriteInfo,filePathAndName): try: with open(filePathAndName,'wb') as fileObj: pickle.dump(needWriteInfo,fileObj) except Exception as e: print(e) #讀取文件內(nèi)容 def ReadObj(filePathAndName): try: with open(filePathAndName,'rb') as fileObj: tmpObj = pickle.load(fileObj) except Exception as e: print(e) else: return tmpObj import json import codecs #寫(xiě)入信息為json文件 def WritInfoAsJson(needWriteInfo,filePathAndName): try: with codecs.open(filePathAndName,'wb',encoding='utf-8') as fileObj: json.dump(needWriteInfo,fileObj) except Exception as e: print(e) #讀取json文件信息 def ReadInfoToJson(filePathAndName): try: with codecs.open(filePathAndName,'rb',encoding='utf-8') as fileObj: tmpJson=json.load(fileObj) except Exception as e: print(e) else: return tmpJson
import FileOPC print('\n寫(xiě)入信息到文件中') filePathAndName2='file/test.txt' tmpstr="測(cè)試寫(xiě)入內(nèi)容abcdefg" FileOPC.WriteInfo(tmpstr,filePathAndName2) print('\n將字符串轉(zhuǎn)為字節(jié)1') tmpbytes1=str.encode('測(cè)試寫(xiě)入內(nèi)容','utf-8') print(tmpbytes1) print('\n將字符串轉(zhuǎn)為字節(jié)2') tmpbytes2=bytes('測(cè)試寫(xiě)入內(nèi)容','utf-8') print(tmpbytes2) print('\n追加信息到文件中') FileOPC.AppedInfo('追加信息123',filePathAndName2) FileOPC.AppedInfo('測(cè)試追加信息456',filePathAndName2) print('\n切分字符串') splitStr="Alice in wonderlan 切割字符串,1,2,3,45,6" tmpSplit = splitStr.split(',') print(tmpSplit) print('\n寫(xiě)入對(duì)象信息到文件') filePathAndName3='file/test2.txt' FileOPC.WriteObj('測(cè)試寫(xiě)入對(duì)象信息112254799abcadshofdsaujfoduasfoj',filePathAndName3) print('\n讀取文件對(duì)象') tmpObj = FileOPC.ReadObj(filePathAndName3) print(tmpObj) import json print('\n寫(xiě)入信息保存為Json文件') filePathAndName4='file/testJson.json' jsonDatas={"101001":[1,3,5,7,9],"101009":["張三","李四",'王五']} #jsonDatas=[2,3,5,7,11,13] FileOPC.WritInfoAsJson(jsonDatas,filePathAndName4) print('\n讀取Json文件信息') tmpJson=FileOPC.ReadInfoToJson(filePathAndName4) print(tmpJson)
讀到這里,這篇“Python怎么實(shí)現(xiàn)文件操作幫助類”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過(guò)才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。