欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

 

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

復制代碼 代碼如下:

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

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

復制代碼 代碼如下:

>>> 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語句處理文件中的每行,最后關閉文件:

復制代碼 代碼如下:

>>> 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對應數(shù)據(jù)分別保存到列表man和other:

復制代碼 代碼如下:

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

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()方法打開磁盤文件時,默認的訪問模式為r,表示讀,不需要特意指定;

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

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

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

 例如,將上例中保存的man和other內容以文件方式保存時,可修改如下:

復制代碼 代碼如下:

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的內容寫到文件中
    print (other, file=other_file)
except IOError:
    print ('File error')
finally:
    man_file.close()
    other_file.close()

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

相關文章

  • Python PyQt5學習之樣式設置詳解

    Python PyQt5學習之樣式設置詳解

    這篇文章主要為大家詳細介紹了Python PyQt5中樣式設置的相關資料,例如為標簽添加背景圖片、為按鈕添加背景圖片、設置窗口透明等,感興趣的可以學習一下
    2022-12-12
  • Python閉包與裝飾器原理及實例解析

    Python閉包與裝飾器原理及實例解析

    這篇文章主要介紹了Python閉包與裝飾器原理及實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-04-04
  • Pytorch搭建YoloV4目標檢測平臺實現(xiàn)源碼

    Pytorch搭建YoloV4目標檢測平臺實現(xiàn)源碼

    這篇文章主要為大家介紹了Pytorch搭建YoloV4目標檢測平臺實現(xiàn)源碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-05-05
  • 舉例講解Python中的list列表數(shù)據(jù)結構用法

    舉例講解Python中的list列表數(shù)據(jù)結構用法

    這篇文章主要介紹了Python中的list列表數(shù)據(jù)結構用法,列表是Python內置的六種集合類數(shù)據(jù)類型中最常見的之一,需要的朋友可以參考下
    2016-03-03
  • python中urlparse模塊介紹與使用示例

    python中urlparse模塊介紹與使用示例

    這篇文章主要給大家介紹了關于python中urlparse模塊介紹與使用的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用python具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。
    2017-11-11
  • Python選擇網(wǎng)卡發(fā)包及接收數(shù)據(jù)包

    Python選擇網(wǎng)卡發(fā)包及接收數(shù)據(jù)包

    今天小編就為大家分享一篇關于Python選擇網(wǎng)卡發(fā)包及接收數(shù)據(jù)包,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-04-04
  • kafka-python批量發(fā)送數(shù)據(jù)的實例

    kafka-python批量發(fā)送數(shù)據(jù)的實例

    今天小編就為大家分享一篇kafka-python批量發(fā)送數(shù)據(jù)的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • 基于Python實現(xiàn)簡易的植物識別小系統(tǒng)

    基于Python實現(xiàn)簡易的植物識別小系統(tǒng)

    這篇文章主要介紹了利用Python實現(xiàn)一個簡易的植物識別系統(tǒng),文中的示例代碼簡潔易懂,對我們學習Python有一定的幫助,需要的小伙伴可以參考一下
    2021-12-12
  • Python判斷操作系統(tǒng)類型代碼分享

    Python判斷操作系統(tǒng)類型代碼分享

    這篇文章主要介紹了Python判斷操作系統(tǒng)類型代碼分享,編寫一些跨平臺程序時經(jīng)常要用到,需要的朋友可以參考下
    2014-11-11
  • Python實現(xiàn)將視頻按照時間維度剪切

    Python實現(xiàn)將視頻按照時間維度剪切

    這篇文章主要為大家詳細介紹了如何利用Python實現(xiàn)將視頻按照時間維度進行剪切,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起了解一下
    2022-12-12

最新評論