Python 語音輸入與檔案
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
語音輸入雜湊資料表
Python 高效率鍵/值雜湊資料表結構稱為「dict」。字典內容可以用大括號 { } 中的一連串鍵/值組合寫成,例如:dict = {key1:value1, key2:value2, ... }。「空白字典」只是一對空白的大括號 {}。
在字典中查詢或設定值會使用方括號,例如:dict['foo'] 查詢索引鍵「foo」下的值。字串、數字和元組可做為鍵,任何類型可以是值。其他類型的鍵不一定能正確運作 (字串和元組無法變更,因此可以保持正常執行)。查詢不在字典中的值會產生 KeyError,請使用「in」請確認鍵是否在字典中,或是使用 dict.get(key),如果鍵不存在,則傳回值或 None (或 get(key, not-found) 可讓您指定要在找不到的情況下傳回什麼值。
## Can build up a dict by starting with the empty dict {}
## and storing key/value pairs into the dict like this:
## dict[key] = value-for-that-key
dict = {}
dict['a'] = 'alpha'
dict['g'] = 'gamma'
dict['o'] = 'omega'
print(dict) ## {'a': 'alpha', 'o': 'omega', 'g': 'gamma'}
print(dict['a']) ## Simple lookup, returns 'alpha'
dict['a'] = 6 ## Put new key/value into dict
'a' in dict ## True
## print(dict['z']) ## Throws KeyError
if 'z' in dict: print(dict['z']) ## Avoid KeyError
print(dict.get('z')) ## None (instead of KeyError)

根據預設,字典上的 for 迴圈會反覆查詢其金鑰。鍵會以任意順序顯示。dict.keys() 和 dict.values() 方法會傳回明確包含的鍵或值清單。此外還有一個 items() 會傳回一個 (鍵、值) 元組清單,這是檢查字典中所有鍵/值資料最有效率的方法。這些清單全都可傳遞到排序() 函式。
## By default, iterating over a dict iterates over its keys.
## Note that the keys are in a random order.
for key in dict:
print(key)
## prints a g o
## Exactly the same as above
for key in dict.keys():
print(key)
## Get the .keys() list:
print(dict.keys()) ## dict_keys(['a', 'o', 'g'])
## Likewise, there's a .values() list of values
print(dict.values()) ## dict_values(['alpha', 'omega', 'gamma'])
## Common case -- loop over the keys in sorted order,
## accessing each key/value
for key in sorted(dict.keys()):
print(key, dict[key])
## .items() is the dict expressed as (key, value) tuples
print(dict.items()) ## dict_items([('a', 'alpha'), ('o', 'omega'), ('g', 'gamma')])
## This loop syntax accesses the whole dict by looping
## over the .items() tuple list, accessing one (key, value)
## pair on each iteration.
for k, v in dict.items(): print(k, '>', v)
## a > alpha o > omega g > gamma
策略注意事項:從績效角度來看,字典是你最重視的工具之一,因此建議你在容易管理資料時輕鬆使用。例如,您可能會讀取記錄檔,其中每一行開頭都是 IP 位址,並使用 IP 位址作為鍵,將資料儲存在字典中,並用其顯示為值的行清單。讀完整個檔案後,您可以查詢任何 IP 位址,並立即看到當中的行清單。這個字典會擷取零散的資料,使資料內容一致。
% 運算子可讓您輕鬆將字典中的值替換成名稱字串:
h = {}
h['word'] = 'garfield'
h['count'] = 42
s = 'I want %(count)d copies of %(word)s' % h # %d for int, %s for string
# 'I want 42 copies of garfield'
# You can also use str.format().
s = 'I want {count:d} copies of {word}'.format(h)
Del
《del》運算子則確實刪除。在最簡單的情況下,它可以移除變數的定義,就像尚未定義該變數一樣。Del 也可用於清單元素或切片中,以便刪除清單中的某個部分,以及從字典中刪除項目。
var = 6
del var # var no more!
list = ['a', 'b', 'c', 'd']
del list[0] ## Delete first element
del list[-2:] ## Delete last two elements
print(list) ## ['b']
dict = {'a':1, 'b':2, 'c':3}
del dict['b'] ## Delete 'b' entry
print(dict) ## {'a':1, 'c':3}
檔案
open() 函式會開啟並傳回可照常讀取或寫入檔案的檔案控制代碼。程式碼 f = open('name', 'r') 會在變數 f 中開啟檔案,以便讀取作業,並在完成時使用 f.close()。應使用「w」,而非「r」編寫程式時,「a」。「迴圈」標準適用於文字檔案,可疊代檔案行 (僅適用於文字檔案,不適用於二進位檔案)。如要查看文字檔中的所有行, for-loop 技術提供簡單又有效率的方法:
# Echo the contents of a text file
f = open('foo.txt', 'rt', encoding='utf-8')
for line in f: ## iterates over the lines of the file
print(line, end='') ## end='' so print does not add an end-of-line char
## since 'line' already includes the end-of-line.
f.close()
在不耗用 10 GB 記憶體的情況下,一次閱讀一行可達到一個最佳品質,不是所有檔案一次需要在記憶體中容納全部的內容。如果在不使用 10 GB 記憶體的情況下,查看 10 GB 檔案中的每一行,就很方便。f.readlines() 方法會將整個檔案讀入記憶體,然後以行清單的形式傳回檔案內容。f.read() 方法可將整個檔案讀取為單一字串,這是一次處理所有文字的便利方法,例如稍後會介紹的規則運算式。
對寫入而言,f.write(string) 方法是將資料寫入開放式輸出檔案最簡單的方法。您也可以使用「print」並開啟「print(string, file=f)」等開啟檔案。
檔案 Unicode
如要讀取及寫入 Unicode 編碼檔案,請使用 `'t'` 模式並明確指定編碼:
with open('foo.txt', 'rt', encoding='utf-8') as f:
for line in f:
# here line is a *unicode* string
with open('write_test', encoding='utf-8', mode='wt') as f:
f.write('\u20ACunicode\u20AC\n') # €unicode€
# AKA print('\u20ACunicode\u20AC', file=f) ## which auto-adds end='\n'
運動增量
建構 Python 程式,不必一次撰寫全部內容。與其只識別第一個里程碑,例如:「第一個步驟是擷取字詞清單」編寫程式碼以取得該里程碑,只在該時間點輸出資料結構,然後執行 sys.exit(0) 即可,讓程式避免執行到沒有完成的部分。等到里程碑代碼正常運作後,即可處理下一個里程碑的程式碼。能夠查看特定狀態下的變數列印出來,有助您思考需要如何轉換這些變數才能達到下一個狀態。Python 利用這個模式可以非常快速,讓您可以稍加變更並執行程式來查看程式的運作方式。利用這種快速交件的方式,只需幾個簡單步驟就能建構您的計畫。
練習:wordcount.py
結合所有基本 Python 教材 (字串、清單、字典、元組、檔案),請嘗試基本練習中的 wordcount.py 摘要練習。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-24 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-24 (世界標準時間)。"],[[["\u003cp\u003ePython uses a "dict" to store key-value pairs, supporting lookups, insertions, and deletions using brackets and built-in methods.\u003c/p\u003e\n"],["\u003cp\u003eIteration over a dict's keys, values, or items is facilitated by methods like \u003ccode\u003ekeys()\u003c/code\u003e, \u003ccode\u003evalues()\u003c/code\u003e, and \u003ccode\u003eitems()\u003c/code\u003e, enabling flexible data processing.\u003c/p\u003e\n"],["\u003cp\u003eFiles in Python are accessed using \u003ccode\u003eopen()\u003c/code\u003e, with modes for reading, writing, and appending, and can be iterated line by line for efficient processing.\u003c/p\u003e\n"],["\u003cp\u003eUnicode files should be opened with the 't' mode and encoding explicitly specified for proper handling of characters.\u003c/p\u003e\n"],["\u003cp\u003eIncremental development is encouraged, where programs are built in stages, focusing on individual milestones and data structure verification.\u003c/p\u003e\n"]]],[],null,["# Python Dict and File\n\nDict Hash Table\n---------------\n\nPython's efficient key/value hash table structure is called a \"dict\". The contents of a dict can be written as a series of key:value pairs within braces { }, e.g. dict = {key1:value1, key2:value2, ... }. The \"empty dict\" is just an empty pair of curly braces {}.\n\nLooking up or setting a value in a dict uses square brackets, e.g. dict\\['foo'\\] looks up the value under the key 'foo'. Strings, numbers, and tuples work as keys, and any type can be a value. Other types may or may not work correctly as keys (strings and tuples work cleanly since they are immutable). Looking up a value which is not in the dict throws a KeyError -- use \"in\" to check if the key is in the dict, or use dict.get(key) which returns the value or None if the key is not present (or get(key, not-found) allows you to specify what value to return in the not-found case). \n\n```python\n ## Can build up a dict by starting with the empty dict {}\n ## and storing key/value pairs into the dict like this:\n ## dict[key] = value-for-that-key\n dict = {}\n dict['a'] = 'alpha'\n dict['g'] = 'gamma'\n dict['o'] = 'omega'\n\n print(dict) ## {'a': 'alpha', 'o': 'omega', 'g': 'gamma'}\n\n print(dict['a']) ## Simple lookup, returns 'alpha'\n dict['a'] = 6 ## Put new key/value into dict\n 'a' in dict ## True\n ## print(dict['z']) ## Throws KeyError\n if 'z' in dict: print(dict['z']) ## Avoid KeyError\n print(dict.get('z')) ## None (instead of KeyError)\n```\n\n\nA for loop on a dictionary iterates over its keys by default. The keys will appear in an arbitrary order. The methods dict.keys() and dict.values() return lists of the keys or values explicitly. There's also an items() which returns a list of (key, value) tuples, which is the most efficient way to examine all the key value data in the dictionary. All of these lists can be passed to the sorted() function. \n\n```python\n ## By default, iterating over a dict iterates over its keys.\n ## Note that the keys are in a random order.\n for key in dict:\n print(key)\n ## prints a g o\n\n ## Exactly the same as above\n for key in dict.keys():\n print(key)\n\n ## Get the .keys() list:\n print(dict.keys()) ## dict_keys(['a', 'o', 'g'])\n\n ## Likewise, there's a .values() list of values\n print(dict.values()) ## dict_values(['alpha', 'omega', 'gamma'])\n\n ## Common case -- loop over the keys in sorted order,\n ## accessing each key/value\n for key in sorted(dict.keys()):\n print(key, dict[key])\n\n ## .items() is the dict expressed as (key, value) tuples\n print(dict.items()) ## dict_items([('a', 'alpha'), ('o', 'omega'), ('g', 'gamma')])\n\n ## This loop syntax accesses the whole dict by looping\n ## over the .items() tuple list, accessing one (key, value)\n ## pair on each iteration.\n for k, v in dict.items(): print(k, '\u003e', v)\n ## a \u003e alpha o \u003e omega g \u003e gamma\n```\n\nStrategy note: from a performance point of view, the dictionary is one of your greatest tools, and you should use it where you can as an easy way to organize data. For example, you might read a log file where each line begins with an IP address, and store the data into a dict using the IP address as the key, and the list of lines where it appears as the value. Once you've read in the whole file, you can look up any IP address and instantly see its list of lines. The dictionary takes in scattered data and makes it into something coherent.\n\nDict Formatting\n---------------\n\nThe % operator works conveniently to substitute values from a dict into a string by name: \n\n```python\n h = {}\n h['word'] = 'garfield'\n h['count'] = 42\n s = 'I want %(count)d copies of %(word)s' % h # %d for int, %s for string\n # 'I want 42 copies of garfield'\n\n # You can also use str.format().\n s = 'I want {count:d} copies of {word}'.format(h)\n```\n\nDel\n---\n\nThe \"del\" operator does deletions. In the simplest case, it can remove the definition of a variable, as if that variable had not been defined. Del can also be used on list elements or slices to delete that part of the list and to delete entries from a dictionary. \n\n```python\n var = 6\n del var # var no more!\n\n list = ['a', 'b', 'c', 'd']\n del list[0] ## Delete first element\n del list[-2:] ## Delete last two elements\n print(list) ## ['b']\n\n dict = {'a':1, 'b':2, 'c':3}\n del dict['b'] ## Delete 'b' entry\n print(dict) ## {'a':1, 'c':3}\n```\n\nFiles\n-----\n\nThe open() function opens and returns a file handle that can be used to read or write a file in the usual way. The code f = open('name', 'r') opens the file into the variable f, ready for reading operations, and use f.close() when finished. Instead of 'r', use 'w' for writing, and 'a' for append. The standard for-loop works for text files, iterating through the lines of the file (this works only for text files, not binary files). The for-loop technique is a simple and efficient way to look at all the lines in a text file: \n\n```python\n # Echo the contents of a text file\n f = open('foo.txt', 'rt', encoding='utf-8')\n for line in f: ## iterates over the lines of the file\n print(line, end='') ## end='' so print does not add an end-of-line char\n ## since 'line' already includes the end-of-line.\n f.close()\n```\n\nReading one line at a time has the nice quality that not all the file needs to fit in memory at one time -- handy if you want to look at every line in a 10 gigabyte file without using 10 gigabytes of memory. The f.readlines() method reads the whole file into memory and returns its contents as a list of its lines. The f.read() method reads the whole file into a single string, which can be a handy way to deal with the text all at once, such as with regular expressions we'll see later.\n\nFor writing, f.write(string) method is the easiest way to write data to an open output file. Or you can use \"print\" with an open file like \"print(string, file=f)\".\n\nFiles Unicode\n-------------\n\nTo read and write unicode encoded files use a \\`'t'\\` mode and explicitly specify an encoding: \n\n```python\nwith open('foo.txt', 'rt', encoding='utf-8') as f:\n for line in f:\n # here line is a *unicode* string\n\nwith open('write_test', encoding='utf-8', mode='wt') as f:\n f.write('\\u20ACunicode\\u20AC\\n') # €unicode€\n # AKA print('\\u20ACunicode\\u20AC', file=f) ## which auto-adds end='\\n'\n```\n\nExercise Incremental Development\n--------------------------------\n\nBuilding a Python program, don't write the whole thing in one step. Instead identify just a first milestone, e.g. \"well the first step is to extract the list of words.\" Write the code to get to that milestone, and just print your data structures at that point, and then you can do a sys.exit(0) so the program does not run ahead into its not-done parts. Once the milestone code is working, you can work on code for the next milestone. Being able to look at the printout of your variables at one state can help you think about how you need to transform those variables to get to the next state. Python is very quick with this pattern, allowing you to make a little change and run the program to see how it works. Take advantage of that quick turnaround to build your program in little steps.\n\nExercise: wordcount.py\n----------------------\n\nCombining all the basic Python material -- strings, lists, dicts, tuples, files -- try the summary **wordcount.py** exercise in the [Basic Exercises](/edu/python/exercises/basic)."]]