《python程式設計從入門到實踐》讀書筆記3

2021-10-11 09:13:03 字數 4504 閱讀 8335

with

open

("c:/users/hasee/desktop/han_demo.txt"

)as my_file:

contents = my_file.read(

)print

(contents)

file

=open

("c:/users/hasee/desktop/demo/han_test_demo.txt"

)content =

file

.read(

)print

(content)

file

.close(

)

上面兩種方法都能正確的開啟檔案並且讀取資料,但是在第一種方法中我們可以發現,我們使用了with關鍵字。關鍵字with在不需要訪問檔案後將其關閉。在這個程式中我們呼叫了open()沒有呼叫close(),我們也可以像第二種那樣呼叫open()和close()來開啟和關閉檔案,但是這樣做時,如果程式存在bug,導致close()語句未執行,檔案將不會被關閉,可能會導致檔案的資料丟失或者受損。

逐行列印,使用for迴圈,而不適用read()語句:

file

="c:/users/hasee/desktop/demo/han_test_demo.txt"

with

open

(file

)as my_file:

for line in my_file:

print

(line.rstrip(

))

使用with關鍵字時,open()返回的檔案物件只在with**塊內可用 ,如果要在with**塊外訪問檔案內容,可以在with**塊內將檔案各行儲存在乙個列表中,並在with**塊外使用該列表。

file

="c:/users/hasee/desktop/demo/han_test_demo.txt"

with

open

(file

)as my_file:

lines = my_file.readlines(

)for line in lines:

print

(line.rstrip(

))

readlines()方法從檔案中讀取每一行,並將其儲存在乙個列表中,接下來,該列表被儲存到變數lines中。

file

="c:/users/hasee/desktop/demo/pi_million_digits.txt"

with

open

(file

)as my_file:

lines = my_file.readlines(

)pi_string =

""for line in lines:

pi_string += line

print

(pi_string[:20

]+"..."

)

在讀取文字時,python將其中的所有文字都解讀為字串,如果要將讀取的數字作為數值使用,就必須使用函式int()將其轉換為整數,或者使用float()轉換為浮點數。

file

="c:/users/hasee/desktop/demo/pi_million_digits.txt"

with

open

(file

)as my_file:

lines = my_file.readlines(

)pi_string =

""for line in lines:

pi_string += line

birthday =

input

("你的生日是:"

)if birthday in pi_string:

print

("你的生日在圓周率裡面!"

)else

:print

("不在。"

)

2023年3月14日,谷歌宣布稱,圓周率現在已經突破3000萬億位,達到了31.4萬億位,這又是乙個巨大的突破,但是,依舊沒有算盡。

要將文字寫入檔案,你在呼叫open()時,需要提供另乙個實參。

split()方法以空格為分隔符,將字串拆成多個部分,並將這些部分都儲存到乙個列表中。

通過split()方法可以查詢出乙個txt檔案有多少個單詞。

def

count_numbers

(filename)

:try

:with

open

(filename)

as book:

book_contents = book.read(

)except

:print

("沒有"

+ filename +

"這本書"

)else

: numbers = book_contents.split(

)print

(filename +

"這本書共有"

+str

(len

(numbers))+

"個單詞"

)file_name =

"c:/users/hasee/desktop/demo/han_book.txt"

count_numbers(file_name)

模組json能夠讓你將簡單的python資料結構轉儲到檔案中,並在程式再次執行時載入該檔案中的資料。

函式json.dump()接收兩個實參:要儲存的資料以及可用於儲存資料的檔案物件

import json

string =[1

,2,3

]# 指定檔名

louise =

"c:/users/hasee/desktop/demo/string.json"

with

open

(louise,

"w")

as loulou:

json.dump(string, loulou)

我們指定了要將該數字列表儲存到其中的檔案的名稱,通常使用副檔名.json來指出檔案儲存的資料為json格式。

使用json.load()可以讀取資料

import json

file_name =

"string.json"

with

open

(file_name)

as loulou:

numbers = json.load(loulou)

print

(numbers)

儲存和讀取使用者生成的資料:

import json

# 為檔案起名

filename =

"username.json"

# 嘗試開啟檔案

try:

with

open

(filename)

as loulou:

username = json.load(loulou)

# 如果之前沒有執行過程式會執行

except filenotfounderror:

username =

input

("請輸入姓名"

)with

open

(filename,

"w")

as loulou:

json.dump(username, loulou)

print

("we will remember you "

+ username +

"!")

# 程式不發生異常會執行的程式

else

:print

("welcome back "

+ username)

我們經常遇到**能夠正常執行,但是需要進一步改進的情況。我們可以將**劃分為完成一系列工作的具體函式,這樣的過程被稱為重構

Python 程式設計 從入門到實踐

1.官網安裝 3.環境配置 務必選中核取方塊add python to path 4.檢視 啟動python版本的命令 python 執行 print hello python world 5.終端執行x.py檔案 python x.py 7.檢視當前目錄中的所有檔案的命令 dir windows系...

Python程式設計從入門到實踐 基礎入門

python程式設計從入門到實踐 基礎入門 1 python中的變數 2 python首字母大寫使用title 方法,全部大寫upper 方法,全部小寫lower 方法 3 python中字串拼接使用 號 4 python中刪除字串的空格 刪除末尾空格的rstrip 刪除開頭空格的lstrip 刪除...

Python程式設計 從入門到實踐 1

內容總結自 python程式設計 從入門到實踐 安裝python3 安裝文字編輯器sublime text並配置python3環境 安裝sublime text tools new build system 將 untitled.sublime build 文件中的所有內容刪除,輸入以下內容 注意,...