Python基礎‘姿勢’7
阿新 • • 發佈:2019-02-17
本章學習系統的json類
import json import pip if __name__ == '__main__': with open('pi_digits.txt') as file_object: contents = file_object.read() print(contents) #檔案路徑 with open('D:\Documents\Downloads\使用前說明.txt') as file_object: contents = file_object.read() print(contents) #逐行讀取 filename = 'pi_digits.txt' with open(filename) as file_object: for line in file_object: print(line) #建立一個包含檔案各行內容的列表 filename = 'pi_digits.txt' with open(filename) as file_object: lines = file_object.readline() for line in lines: print(line.rstrip()) #使用檔案的內容 #包含一百萬位的大型檔案 #寫入檔案 '''儲存資料的最簡單的方式之一是將其寫入到檔案中。 通過將輸出寫入檔案, 即便關閉包含程式輸出的終端視窗, 這些輸出也依然存在: 你可以在程式結束執行後檢視這些輸出, 可與別人分享輸出檔案, 還可編寫程式來將這些輸出讀取到記憶體中並進行處理''' filename = 'programming.txt' with open(filename, 'w') as file_object: file_object.write("I love programming.and i love ios ") #寫入多行 filename = 'programming.txt' with open(filename,'w') as file_object: file_object.write("I love programming.") file_object.write("I love creating new games.") #附加到檔案 '''如果你要給檔案新增內容, 而不是覆蓋原有的內容, 可以附加模式 開啟檔案。 你以附加模式開啟檔案時, Python不會在返回檔案物件前清空檔案, 而你寫入到檔案的行都將新增 到檔案末尾。 如果指定的檔案不存在, Python將為你建立一個空檔案''' filename = 'programming.txt' with open(filename, 'a') as file_object: file_object.write("I also love finding meaning in large datasets.\n") file_object.write("I also love finding meaning in large datasets.\n") #異常的處理 try: print(5/0) except ZeroDivisionError: print("You can't divide by zero!") #使用異常避免崩潰 print("Give me two numbers, and I'll divide them.") print("Enter 'q' to quit.") # while True: # first_number = input("\nFirst number: ") # if first_number == 'q': # break # second_number = input("Second number: ") # if second_number == 'q': # break # answer = int(first_number) / int(second_number) # print(answer) #處理FileNotFoundError 異常 '''使用檔案時, 一種常見的問題是找不到檔案: 你要查詢的檔案可能在其他地方、 檔名可能不正確或者這個檔案根本就不存在。 對於所有這些情形, 都可使用try-except 程式碼 塊以直觀的方式進行處理''' filename = 'alice.txt' try: with open(filename) as f_obj: contents = f_obj.read() except FileNotFoundError: msg = "Sorry, the file " + filename + " does not exist." print(msg) def count_words(filename): """計算一個檔案大致包含多少個單詞""" try: with open(filename) as f_obj: contents = f_obj.read() except FileNotFoundError: msg = "Sorry, the file " + filename + " does not exist." print(msg) else: # 計算檔案大致包含多少個單詞 words = contents.split() num_words = len(words) print("The file " + filename + " has about " + str(num_words) + " words.") filename = 'programming.txt' count_words(filename) #儲存資料 '''模組json 讓你能夠將簡單的Python資料結構轉儲到檔案中, 並在程式再次執行時載入該檔案中的資料。 你還可以使用json 在Python程式之間分享資料。 更重要的是, JSON資料 格式並非Python專用的, 這讓你能夠將以JSON格式儲存的資料與使用其他程式語言的人分享。 這是一種輕便格式, 很有用, 也易於學習''' #使用json.dump() 和json.load() numbers = [2, 3, 5, 7, 11, 13] filename = 'numbers.json' with open(filename, 'w') as f_obj: json.dump(numbers, f_obj) print(numbers) filename = 'numbers.json' with open(filename) as f_obj: numbers = json.load(f_obj) print(numbers) #儲存和讀取使用者生成的資料 '''對於使用者生成的資料, 使用json 儲存它們大有裨益, 因為如果不以某種方式進行儲存, 等程式停止執行時使用者的資訊將丟失''' username = input("What is your name? ") filename = 'username.json' # with open(filename, 'w') as f_obj: # json.dump(username, f_obj) # print("We'll remember you when you come back, " + username + "!") with open(filename) as f_obj: username = json.load(f_obj) print("Welcome back, " + username + "!") #重構 print(pip.pep425tags.get_supported()) pass