Python實現(xiàn)文件操作幫助類的示例代碼
一、業(yè)務(wù)需求
在使用Python進行業(yè)務(wù)開發(fā)的時候,需要將一些數(shù)據(jù)保存到本地文件存儲,方便后面進行數(shù)據(jù)分析展示。
二、需求分析
通過查看需求可得出:需要將數(shù)據(jù)存儲為本地文件(這就是涉及到文件的操作),文件操作屬于基礎(chǔ)內(nèi)容,可以直接將常用的文件操作封裝為一個文件,后面使用直接調(diào)用即可。
三、實現(xiàn)方法
3.1、Python文件幫助類
#文件操作 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 #寫入信息到文件(覆蓋原有內(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) #寫入對象到文件 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 #寫入信息為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
3.2、Python文件幫助類的使用示例
import FileOPC print('\n寫入信息到文件中') filePathAndName2='file/test.txt' tmpstr="測試寫入內(nèi)容abcdefg" FileOPC.WriteInfo(tmpstr,filePathAndName2) print('\n將字符串轉(zhuǎn)為字節(jié)1') tmpbytes1=str.encode('測試寫入內(nèi)容','utf-8') print(tmpbytes1) print('\n將字符串轉(zhuǎn)為字節(jié)2') tmpbytes2=bytes('測試寫入內(nèi)容','utf-8') print(tmpbytes2) print('\n追加信息到文件中') FileOPC.AppedInfo('追加信息123',filePathAndName2) FileOPC.AppedInfo('測試追加信息456',filePathAndName2) print('\n切分字符串') splitStr="Alice in wonderlan 切割字符串,1,2,3,45,6" tmpSplit = splitStr.split(',') print(tmpSplit) print('\n寫入對象信息到文件') filePathAndName3='file/test2.txt' FileOPC.WriteObj('測試寫入對象信息112254799abcadshofdsaujfoduasfoj',filePathAndName3) print('\n讀取文件對象') tmpObj = FileOPC.ReadObj(filePathAndName3) print(tmpObj) import json print('\n寫入信息保存為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)
3.3、示例執(zhí)行結(jié)果
到此這篇關(guān)于Python實現(xiàn)文件操作幫助類的示例代碼的文章就介紹到這了,更多相關(guān)Python文件操作幫助類內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)將序列分解為單獨變量的方法
這篇文章主要介紹了Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)將序列分解為單獨變量的方法,結(jié)合實例形式分析了Python序列賦值實現(xiàn)的分解成單獨變量功能相關(guān)操作技巧,需要的朋友可以參考下2018-02-02