python資料儲存到檔案

2021-10-24 11:18:23 字數 3991 閱讀 9474

1、使用open與print進行資料儲存到檔案

#filename列表形式檔名

def write_file(filename):

try:

for item_name in filename:

out_file=open(item_name,'w') #寫模式開啟檔案,並賦值至檔案物件

data='this is'+item_name

print(data,file=out_file) #將data資料儲存到指定檔案

out_file.close() #關閉檔案

except ioerror:

print('file error!')

if __name__=='__main__':

write_file(['c:/users/administrator/desktop/out.txt'])

2、上述**改進,有一些**不論出現什麼錯誤都必須執行,可以向try語句的finally組增加**

#filename列表形式檔名

def write_file(filename):

#存放輸出檔案

out_file_list=

try:

for item_name in filename:

out_file=open(item_name,'w')

data='this is'+item_name

print(data,file=out_file)

except ioerror:

print('file error!')

finally:

for out_file_item in out_file_list:

out_file_item.close()

if __name__=='__main__':

write_file(['c:/users/administrator/desktop/out.txt'])

[^使用try/except/finally模式處理檔案相當常用]: 

3、使用with處理檔案,使用with不需要再使用finally組處理檔案關閉,python直譯器會自動為你考慮。

#filename列表形式檔名

def write_file_use_with(filename):

#存放輸出檔案

out_file_list=

try:

for item_name in filename:

with open(item_name,'w') as out_file:

data='this is'+item_name

print(data,file=out_file)

except ioerror as err:

print('file error:'+str(err))

if __name__=='__main__':

write_file_use_with(['c:/users/administrator/desktop/out.txt'])

[^with語句利用一種名為上下文管理協議的python技術]: 

4、修改檔案寫的格式,重新定義print寫的方法

#按格式輸出列表,若遇見列表進行縮排。

#indent:是否進行縮排

#level:縮排幾個製表符

#fn:文字輸出位置(預設螢幕列印)

def print_handle_list(the_list,indent=false,level=0,fn=sys.stdout):

for item in the_list:

if isinstance(item,list):

print_handle_list(item,indent,level+1,fn)

else:

if indent:

for tab in range(level):

print("\t",end='',file=fn)

print(item,file=fn)

#filename列表形式檔名

def write_file_use_with(filename):

#存放輸出檔案

out_file_list=

try:

for item_name in filename:

with open(item_name,'w') as out_file:

data='this is'+item_name

print_handle_list(data,fn=out_file) #呼叫新的print方法

except ioerror as err:

print_handle_list('file error:'+str(err))

5、python提供了乙個標準庫,名稱為pickle,它可以儲存和載入幾乎任何python資料物件。

6、使用dump儲存資料(必須要以二進位制訪問模式開啟檔案)-「醃製「:將資料物件儲存到乙個持久儲存中的過程

#引入pickle模組

import pickle

#filename列表形式檔名

def write_file_use_pickle(filename):

#存放輸出檔案

out_file_list=

try:

for item_name in filename:

#wb:寫二進位制模式

with open(item_name,'wb') as out_file:

data=['this is'+item_name,'test_pickle']

pickle.dump(data,out_file) #呼叫pickle的dump方法

except ioerror as err:

print('file error:'+str(err))

except pickle.pickleerror as perr:

print('pickling error:'+str(perr))

print(out_file_list)

if __name__=='__main__':

write_file_use_pickle(['c:/users/administrator/desktop/out.txt'])

7、使用load恢復資料(必須要以二進位制訪問模式開啟檔案)-」解除醃製「:從持久儲存中恢復乙個已儲存的資料物件的過程

#引入pickle模組

import pickle

#filename列表形式檔名

def read_file_use_pickle(filename):

try:

for item_name in filename:

#wb:寫二進位制模式

記憶體資料儲存到檔案

include include int main void tt 定義結構體 tt temp temp.a 10 temp.b 20 strcpy temp.buf,hello 為結構體賦值 int c sizeof temp stream fopen at.dat w 開啟檔案 fwrite te...

Python爬蟲 爬蟲獲取資料儲存到檔案

本篇文章 繼續介紹另外兩種方式來實現python爬蟲獲取資料,並將python獲取的資料儲存到檔案中。說明一下我的 環境是python3.7,本地環境是python2.x的可能需要改部分 用python3.x環境的沒問題。coding utf 8 import urllib.requestimpor...

python將資料儲存到csv檔案中

假如有乙個python 執行出來後,其結果是乙個二維列表,怎麼樣把這些東西儲存到 當中呢?由如下 可得 import csvn 路飛 男 100 索隆 男 99 娜美 女 90 b 姓名 性別 分數 with open 統計.csv w newline as t numline是來控制空的行數的 w...