最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

從零學(xué)python系列之從文件讀取和保存數(shù)據(jù)

 更新時(shí)間:2014年05月23日 10:23:38   作者:  
在Python一般都是運(yùn)用內(nèi)置函數(shù)open()與文件進(jìn)行交互,下面說說具體用法

在HeadFirstPython網(wǎng)站中下載所有文件,解壓后以chapter 3中的“sketch.txt”為例:

 

新建IDLE會話,首先導(dǎo)入os模塊,并將工作目錄卻換到包含文件“sketch.txt”的文件夾,如C:\\Python33\\HeadFirstPython\\chapter3

復(fù)制代碼 代碼如下:

>>> import os
>>> os.getcwd()    #查看當(dāng)前工作目錄
'C:\\Python33'
>>> os.chdir('C:/Python33/HeadFirstPython/chapter3')   #切換包含數(shù)據(jù)文件的文件夾
>>> os.getcwd()     #查看切換后的工作目錄
'C:\\Python33\\HeadFirstPython\\chapter3'

打開文件“sketch.txt”,讀取并顯示前兩行:

復(fù)制代碼 代碼如下:

>>> data=open('sketch.txt')
>>> print(data.readline(),end='')
Man: Is this the right room for an argument?
>>> print(data.readline(),end='')
Other Man: I've told you once.

回到文件起始位置,使用for語句處理文件中的每行,最后關(guān)閉文件:

復(fù)制代碼 代碼如下:

>>> data.seek(0)   #使用seek()方法回到文件起始位置
>>> for each_line in data:
    print(each_line,end='')

   
Man: Is this the right room for an argument?
Other Man: I've told you once.
Man: No you haven't!
Other Man: Yes I have.
Man: When?
Other Man: Just now.
Man: No you didn't!
Other Man: Yes I did!
Man: You didn't!
Other Man: I'm telling you, I did!
Man: You did not!
Other Man: Oh I'm sorry, is this a five minute argument, or the full half hour?
Man: Ah! (taking out his wallet and paying) Just the five minutes.
Other Man: Just the five minutes. Thank you.
Other Man: Anyway, I did.
Man: You most certainly did not!
Other Man: Now let's get one thing quite clear: I most definitely told you!
Man: Oh no you didn't!
Other Man: Oh yes I did!
Man: Oh no you didn't!
Other Man: Oh yes I did!
Man: Oh look, this isn't an argument!
(pause)
Other Man: Yes it is!
Man: No it isn't!
(pause)
Man: It's just contradiction!
Other Man: No it isn't!
Man: It IS!
Other Man: It is NOT!
Man: You just contradicted me!
Other Man: No I didn't!
Man: You DID!
Other Man: No no no!
Man: You did just then!
Other Man: Nonsense!
Man: (exasperated) Oh, this is futile!!
(pause)
Other Man: No it isn't!
Man: Yes it is!
>>> data.close()

讀取文件后,將不同role對應(yīng)數(shù)據(jù)分別保存到列表man和other:

復(fù)制代碼 代碼如下:

import os
print(os.getcwd())
os.chdir('C:\Python33\HeadFirstPython\chapter3')
man=[]    #定義列表man接收Man的內(nèi)容
other=[]  #定義列表other接收Other Man的內(nèi)容

try:
    data=open("sketch.txt")
    for each_line in data:
        try:
            (role, line_spoken)=each_line.split(':', 1)
            line_spoken=line_spoken.strip()
            if role=='Man':
                man.append(line_spoken)
            elif role=='Other Man':
                other.append(line_spoken)
        except ValueError:
                pass
    data.close()
except IOError:
    print('The datafile is missing!')
print (man)
print (other)

Tips:

使用open()方法打開磁盤文件時(shí),默認(rèn)的訪問模式為r,表示讀,不需要特意指定;

要打開一個(gè)文件完成寫,需要指定模式w,如data=open("sketch.txt","w"),如果該文件已經(jīng)存在則會清空現(xiàn)有內(nèi)容;

要追加到一個(gè)文件,需要指定模式a,不會清空現(xiàn)有內(nèi)容;

要打開一個(gè)文件完成寫和讀,且不清空現(xiàn)有內(nèi)容,需要指定模式w+;

 例如,將上例中保存的man和other內(nèi)容以文件方式保存時(shí),可修改如下:

復(fù)制代碼 代碼如下:

import os
print(os.getcwd())
os.chdir('C:\Python33\HeadFirstPython\chapter3')
man=[]
other=[]

try:
    data=open("sketch.txt")
    for each_line in data:
        try:
            (role, line_spoken)=each_line.split(':', 1)
            line_spoken=line_spoken.strip()
            if role=='Man':
                man.append(line_spoken)
            elif role=='Other Man':
                other.append(line_spoken)
        except ValueError:
                pass
    data.close()
except IOError:
    print('The datafile is missing!')

try:
    man_file=open('man.txt', 'w')      #以w模式訪問文件man.txt
    other_file=open('other.txt','w')   #以w模式訪問文件other.txt
    print (man, file=man_file)           #將列表man的內(nèi)容寫到文件中
    print (other, file=other_file)
except IOError:
    print ('File error')
finally:
    man_file.close()
    other_file.close()

但是第26行print()為什么會報(bào)錯(cuò)?“syntax error while detecting tuple”,有大神能給解惑一下不

相關(guān)文章

  • unittest+coverage單元測試代碼覆蓋操作實(shí)例詳解

    unittest+coverage單元測試代碼覆蓋操作實(shí)例詳解

    這篇文章主要為大家詳細(xì)介紹了unittest+coverage單元測試代碼覆蓋操作的實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • Python實(shí)現(xiàn)切割mp3片段并降低碼率

    Python實(shí)現(xiàn)切割mp3片段并降低碼率

    MoviePy是一個(gè)基于Python的視頻編輯庫,它提供了創(chuàng)建、編輯、合并、剪輯和轉(zhuǎn)換視頻的功能,所以本文主要介紹如何使用moviepy來分割音頻流并降低碼率,感興趣的可以了解下
    2023-08-08
  • python實(shí)現(xiàn)模擬鍵盤鼠標(biāo)重復(fù)性操作Pyautogui

    python實(shí)現(xiàn)模擬鍵盤鼠標(biāo)重復(fù)性操作Pyautogui

    這篇文章主要為大家詳細(xì)介紹了python如何利用Pyautogui模擬鍵盤鼠標(biāo)重復(fù)性操作,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-11-11
  • 教你用python將數(shù)據(jù)寫入Excel文件中

    教你用python將數(shù)據(jù)寫入Excel文件中

    Python作為一種腳本語言相較于shell具有更強(qiáng)大的文件處理能力,下面這篇文章主要給大家介紹了關(guān)于如何用python將數(shù)據(jù)寫入Excel文件中的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-02-02
  • python爬蟲入門教程之糗百圖片爬蟲代碼分享

    python爬蟲入門教程之糗百圖片爬蟲代碼分享

    這篇文章主要介紹了python爬蟲入門教程之糗百圖片爬蟲代碼分享,本文以抓取糗事百科內(nèi)涵圖為需求寫了一個(gè)爬蟲,,需要的朋友可以參考下
    2014-09-09
  • python免殺技術(shù)shellcode的加載與執(zhí)行

    python免殺技術(shù)shellcode的加載與執(zhí)行

    本文主要介紹了python免殺技術(shù)shellcode的加載與執(zhí)行,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • python-opencv如何讀取圖片及尺寸修改

    python-opencv如何讀取圖片及尺寸修改

    這篇文章主要介紹了python-opencv如何讀取圖片及尺寸修改,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • python計(jì)算機(jī)視覺OpenCV入門講解

    python計(jì)算機(jī)視覺OpenCV入門講解

    這篇文章主要介紹了python計(jì)算機(jī)視覺OpenCV入門講解,關(guān)于圖像處理的相關(guān)簡單操作,包括讀入圖像、顯示圖像及圖像相關(guān)理論知識
    2022-06-06
  • Python批量獲取基金數(shù)據(jù)的方法步驟

    Python批量獲取基金數(shù)據(jù)的方法步驟

    這篇文章主要介紹了Python批量獲取基金數(shù)據(jù)的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • python程序快速縮進(jìn)多行代碼方法總結(jié)

    python程序快速縮進(jìn)多行代碼方法總結(jié)

    在本篇文章里小編給大家整理了關(guān)于python程序如何快速縮進(jìn)多行代碼的相關(guān)知識點(diǎn),需要的朋友們學(xué)習(xí)下。
    2019-06-06

最新評論

太湖县| 荃湾区| 若尔盖县| 和顺县| 桃江县| 中西区| 沂水县| 琼海市| 理塘县| 周至县| 大邑县| 迭部县| 青海省| 潜江市| 辽中县| 肃南| 徐闻县| 南召县| 宁城县| 怀柔区| 黔南| 鄯善县| 毕节市| 方正县| 灵宝市| 铁岭县| 湘潭县| 鲜城| 嘉兴市| 安化县| 玛曲县| 杭锦旗| 嘉定区| 阳信县| 宜君县| 杭锦后旗| 清镇市| 岗巴县| 柞水县| 武平县| 柘荣县|