国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

head first python 第四章

2019-11-08 02:10:19
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

第四章 主題為用持久存儲(chǔ)!這里存儲(chǔ)的地方是本文件。 這里介紹了本地文件的基本操作方法;以及用打開(kāi)文件python提供的幾種方式,包括一些細(xì)節(jié)。來(lái)一一展開(kāi)。 先看整體代碼。 代碼分為兩塊 第一個(gè)try except 語(yǔ)句塊是用來(lái)讀取文件‘sketch.txt’文件的中的數(shù)據(jù),分別存放與man與other這兩個(gè)list里面。 第二個(gè)語(yǔ)句塊 把man與other分別放入man_data.txt 與other_data.txt 兩個(gè)文件。

man=[]other=[]import osos.chdir('D:/python/chapter3')os.getcwd()try: data=open('sketch.txt','r'); for each_line in data: try: 來(lái)說(shuō)細(xì)節(jié) 1. 文件的打開(kāi)方式

data=open('sketch.txt','r');

open(file, mode=’r’, buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None) Open file and return a stream. Raise IOError upon failure. 之所以用stream來(lái)說(shuō)明這里的返回值,是因?yàn)樵?rb 模式下返回的二進(jìn)制,在rt(t為默認(rèn)) 返回的是string。(** text mode (the default, or when ‘t’ is appended to the mode argument), the contents of the file are returned as strings, t**) 但文件讀取的數(shù)據(jù)能用for get in data 直接來(lái)遍歷;在一般在str中添加幾個(gè)’/n’卻不行。那么這里背后一定做了某種處理

=============================================================== Character Meaning ——— ————————————————————— ‘r’ open for reading (default) ‘w’ open for writing, truncating the file first ‘x’ create a new file and open it for writing ‘a(chǎn)’ open for writing, appending to the end of the file if it exists ‘b’ binary mode ‘t’ text mode (default) ‘+’ open a disk file for updating (reading and writing) ‘U’ universal newline mode (deprecated) ========= =============================================================== The default mode is ‘rt’ (open for reading text). For binary random access, the mode ‘w+b’ opens and truncates the file to 0 bytes, while ‘r+b’ opens the file without truncation. The ‘x’ mode implies ‘w’ and raises an FileExistsError if the file already exists.

data.readline()返回一行data.seek(0)文件指針?lè)祷爻跏嘉恢?p>2.try except finally 語(yǔ)句塊

for each_line in data: try: print(each_line,end='') (who,who_talk)=each_line.split(':',1) if(who=='Man'): man.append(who_talk) if(who=='Other Man'): other.append(who_talk) except ValueError: pass

循環(huán)體內(nèi)的try except ;比if else語(yǔ)句包容性好;做完判斷后還能保證for循環(huán)的執(zhí)行,很不錯(cuò)哦!

3.文件的更新 在這里在強(qiáng)調(diào)一下,文件修改后只有close才能存儲(chǔ)完數(shù)據(jù)。每個(gè)文件都需要關(guān)閉~。。 w模式會(huì)刪除全部數(shù)據(jù) w+會(huì)在數(shù)據(jù)后添加 a創(chuàng)建文件,如果存在會(huì)拋出異常。 當(dāng)然存在文件關(guān)兩次的可能性,那么在這里如何判斷呢?

if 'man_file' in locals(): man_file.close()

locals返回的是一個(gè)字典,包含了本地的所有變量~。 但是不支持遍歷,因?yàn)槊看伪闅v時(shí)都會(huì)產(chǎn)生新的遍歷而改變locals返回的對(duì)應(yīng)值。 那么如果想遍歷有方法嗎? 有的

用copyimport copyx = copy.copy(locals())x = copy.deepcopy(locals()) 淺拷貝與深拷貝。淺拷貝只拷貝了當(dāng)前指針?biāo)赶驅(qū)ο蟮闹担徊还苁侵颠€是引用。 深拷貝,將引用與引用的引用的值。。都進(jìn)行拷貝。

額,集合類(lèi)型貌似要單開(kāi)一篇來(lái)搞搞。增、刪、查、遍歷各記錄一下~


發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 广平县| 凭祥市| 兴和县| 涿州市| 河西区| 大渡口区| 宿松县| 洛南县| 迁西县| 郁南县| 扶风县| 泰宁县| 格尔木市| 都昌县| 芮城县| 花莲县| 尼勒克县| 娄烦县| 会宁县| 惠水县| 广东省| 霞浦县| 宜君县| 广昌县| 临泽县| 北宁市| 东至县| 崇阳县| 麻江县| 巴马| 高碑店市| 荣成市| 南昌县| 荣成市| 晋州市| 屯昌县| 霍州市| 古交市| 长宁区| 十堰市| 云南省|