小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

python進(jìn)階學(xué)習(xí)筆記(一)

 小豬窩969 2015-01-08

同樣是《python基礎(chǔ)教程(第二版)》的內(nèi)容,只是后面內(nèi)容學(xué)起來,相比前面會(huì)比較有趣,也更加實(shí)用,所以,將“基礎(chǔ)”改為“進(jìn)階”。

python 電子書分享地址:http:///Q2U87uGrNiTA3

 

本節(jié)講文件的操作

-------------------------------

 

打開文件

 

open函數(shù)用來打開文件,語法如下:

open(name[, mode[,buffering]])

open函數(shù)使用一個(gè)文件名作為唯一的強(qiáng)制參數(shù),然后返回一個(gè)文件對(duì)象。假設(shè)我要打開我硬盤(I/python/test.txt) 文件,可以用下面方法:

>>> f = open(r'i:\python\test.txt')

 

open函數(shù)中模式參數(shù)的常用值

 

 

基本文件方法

 

打開文件是第一步,下面就需要對(duì)文件進(jìn)行讀或?qū)?,可?span style="font-family: 'Times New Roman';">write 和 read方法進(jìn)行讀或?qū)憽?/span>

復(fù)制代碼
#寫入文件內(nèi)容
>>> f = open('test.txt','w')
>>> f.write('hello,')
>>> f.write('world!')
>>> f.close()

# 讀取文件內(nèi)容
>>> f = open('test.txt','r')
>>> f.read(4)   #讀取前4個(gè)字符
'hell'
>>> f.read()   #讀取剩余的所有字符
'o,world!'
復(fù)制代碼

 

關(guān)閉文件

  應(yīng)該牢記使用close方法關(guān)閉文件。盡管一個(gè)文件對(duì)象在退出程序后會(huì)自動(dòng)關(guān)閉,但關(guān)閉文件是沒什么害處的,可以避免在某些操作系統(tǒng)或設(shè)置中進(jìn)行無用的修改,這樣做也會(huì)避免用完系統(tǒng)中打開文件的配額。

 

 

使用基本文件方法

 

假如test.txt文件包含如下內(nèi)容:

-----------------------------

Welcome to this file

There is nothing here except

This stupid haiku

-----------------------------

下面是基本讀文件的方法:

復(fù)制代碼
# read(n) 指定參數(shù)
>>> f = open(r'I:\python\test.txt')
>>> f.read(7)
'Welcome'
>>> f.read(4)
' to '
>>> f.close()

# read() 不指定參數(shù)
>>> f = open(r'I:\python\test.txt')
>>> print f.read()
Welcome to this file
There is nothing here except
This stupid haiku
>>> f.close()

# readline() 
>>> f = open(r'I:\python\test.txt')
>>> for i in range(3):
    print str(i) + ':' + f.readline()

    
0:Welcome to this file

1:There is nothing here except

2:This stupid haiku
>>> f.close()

#readlines()
>>> import pprint
>>> pprint.pprint(open(r'I:\python\test.txt').readlines())
['Welcome to this file\n',
 'There is nothing here except\n',
 'This stupid haiku']
復(fù)制代碼

readline返回一行的字符串, readlines返回包含文件所有內(nèi)容的字符串列表每個(gè)元素是一行的字符串

pprint 模塊的pprint方法將內(nèi)容分成每個(gè)小項(xiàng)單行顯示。

 

下面是寫文件的基本方法:

>>> f = open(r'I:\python\test.txt','w')  #默認(rèn)是讀文件,可以不加‘r’,寫文件一定要加’w’
>>> f.write('this\nis no \nhaiku')
>>> f.close()

 

復(fù)制代碼
>>> f = open(r'I:\python\test.txt')
>>> lines = f.readlines()
>>> lines[1] = "isn't a\n"
>>> f = open(r'I:\python\test.txt','w')
>>> f.writelines(lines)
>>> f.close()
復(fù)制代碼

 

 

對(duì)文件內(nèi)容進(jìn)行迭代

 

1、接字節(jié)處理

最常見的對(duì)文件內(nèi)容進(jìn)行迭代的方法是while循環(huán)中使用read方法。例如,對(duì)每個(gè)字符進(jìn)行循環(huán),可以用下面方法實(shí)現(xiàn):

復(fù)制代碼
f = open(filename)
char = f.read(1)
while char:
    process(char)
    char = f.read(1)
f.close()
復(fù)制代碼

read方法返回的字符串會(huì)包含一個(gè)字符,直到文件末尾,read返回一個(gè)空的字符串,char將變?yōu)榧佟?/span>

可以看到,char = f.read(1) 被重復(fù)地使用,代碼重復(fù)通過被認(rèn)為是一件壞事,看看下面的方法:

復(fù)制代碼
f = open(filename)

while True:
    char = f.read(1)
    if not char: break
    process(char)
f.close()
復(fù)制代碼

這里break語句被頻繁的使用(這樣會(huì)讓代碼比較難懂),盡管如此,但它仍然要比前面的方法好。

 

2、讀取所有內(nèi)容

如果文件不是很大,那么可以使用不帶參數(shù)的read方法一次讀取整個(gè)文件,或者使用readlines方法。

復(fù)制代碼
#用read迭代每個(gè)字符
f = open(filename)
for char in f.read():
    process(char)
f.close()

#用readlines迭代行: f = open(filename) for line in f.readlines(): process(line) f.close()
復(fù)制代碼

 

3、用fileinput 來進(jìn)行迭代

fileinput模塊包含了打開文件的函數(shù),,只需要傳一個(gè)文件名給它

import fileinput
for line in fileinput.input(filename):
    process(line)

 

4、文件迭代器

好吧!這是python2.2之后才有的方法,如果它一開始就有,上面的方法也許就不存在了。文件對(duì)象是可以迭代的,這就意味著可以直接在for循環(huán)中對(duì)他們進(jìn)行迭代

f = open(filename)
for line in f:
    process(line)
f.close()

 

 

再來看下面例子:

復(fù)制代碼
>>> f = open(r'I:\python\test.txt','w')
>>> f.write('First line\n')
>>> f.write('Second line\n')
>>> f.write('Third line\n')
>>> f.close()
>>> lines = list(open(r'I:\python\test.txt'))
>>> lines
['First line\n', 'Second line\n', 'Third line\n']
>>> first,second,third = open(r'I:\python\test.txt')
>>> first
'First line\n'
>>> second
'Second line\n'
>>> third
'Third line\n'
復(fù)制代碼

在這個(gè)例子中:

 

  •  使用序列來對(duì)一個(gè)打開的文件進(jìn)行解包操作,把每行都放入一個(gè)單獨(dú)的變理中,這么做是很有實(shí)用性的,因?yàn)橐话悴恢牢募杏卸嗌傩校菔镜奈募摹暗浴薄?/span>
  •  在寫文件后一定要關(guān)閉文件,這樣才能確保數(shù)據(jù)被更新到硬盤。

 

 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多