เราสามารถใช้วิธี:
inputfile = open(filename)
for line in inputfile:
work_on_the_line
เช่นโค้ดข้างล่างเปิดไฟล์ `/usr/share/dict/words` แล้วอ่านทีละบรรทัด โดยที่แต่ละบรรทัดอยู่ในตัวแปร `word` ด้วยคำสั่ง `for word in inputfile:`
#นับคำที่ขึ้นต้นด้วยอักษร e หรือ E
inputfile = open('/usr/share/dict/words') #เปิดไฟล์ที่มีคำภาษาอังกฤษบรรทัดละหนึ่งคำ
count = 0 #เริ่มนับ ยังไม่่เจออะไรเลย ดังนั้น count = 0
for word in inputfile: #อ่านเข้ามาทีละบรรทัด เก็บบรรทัดไว้ในตัวแปร word
word = word.strip() #strip() จะลบตัวอักษรที่มองไม่เห็น (white spaces) ด้านหน้าและด้านหลังของคำ รวมถึงตัวอักษรขึ้นบรรทัดใหม่ (newline, \n)
if word.startswith("e") or word.startswith("E"): #ถ้าเจอบรรทัดที่ขึ้นต้นด้วย e หรือ E ให้นับเพิ่มเข้าไปใน count
#print(word)
count = count + 1
print(f"Number of words is {count}") #รายงานผลนับใน count
Number of words is 8736
#วิธีนี้อ่านไฟล์ /usr/share/dict/words ซ้ำๆถึง 26 รอบ
for letter in "abcdefghijklmnopqrstuvwxyz":
inputfile = open('/usr/share/dict/words')
count = 0
for word in inputfile:
word = word.strip()
if word.startswith(letter):
count = count + 1
print(f"There are {count} words starting with the letter {letter}")
There are 14537 words starting with the letter a There are 9675 words starting with the letter b There are 17406 words starting with the letter c There are 9946 words starting with the letter d There are 7818 words starting with the letter e There are 6382 words starting with the letter f There are 5843 words starting with the letter g There are 7889 words starting with the letter h There are 8303 words starting with the letter i There are 1158 words starting with the letter j There are 1735 words starting with the letter k There are 5211 words starting with the letter l There are 10709 words starting with the letter m There are 6098 words starting with the letter n There are 7219 words starting with the letter o There are 22171 words starting with the letter p There are 1075 words starting with the letter q There are 8955 words starting with the letter r There are 22759 words starting with the letter s There are 11389 words starting with the letter t There are 16179 words starting with the letter u There are 3079 words starting with the letter v There are 3607 words starting with the letter w There are 293 words starting with the letter x There are 532 words starting with the letter y There are 719 words starting with the letter z
#วิธีนี้อ่านไฟล์ /usr/share/dict/words รอบเดียว
#ใช้ตัีวแปร count แบบประเภท dictionary (หรือเรียกอีกอย่างว่า hashed table)
inputfile = open('/usr/share/dict/words')
count = {} # {} แปลว่า count เป็น dictionary สามารถถูกอ้างอิงด้วยตัวอักษรและอื่นๆ ไม่จำเป็นต้องเป็นตัวเลขแบบถ้า count เป็น array
lowercase_letters = "abcdefghijklmnopqrstuvwxyz" #อักษรภาษาอังกฤษ 26 ตัว
for letter in lowercase_letters:
count[letter] = 0 #เริ่มต้นให้จำนวนคำของทุกตัวอักษรเป็นศูนย์ก่อน
for word in inputfile:
word = word.strip()
for letter in lowercase_letters:
if word.startswith(letter):
count[letter] += 1 #เหมือนกับ count[letter] = count[letter] + 1
for letter, count in count.items():
print(f"There are {count} words starting with the letter {letter}")
There are 14537 words starting with the letter a There are 9675 words starting with the letter b There are 17406 words starting with the letter c There are 9946 words starting with the letter d There are 7818 words starting with the letter e There are 6382 words starting with the letter f There are 5843 words starting with the letter g There are 7889 words starting with the letter h There are 8303 words starting with the letter i There are 1158 words starting with the letter j There are 1735 words starting with the letter k There are 5211 words starting with the letter l There are 10709 words starting with the letter m There are 6098 words starting with the letter n There are 7219 words starting with the letter o There are 22171 words starting with the letter p There are 1075 words starting with the letter q There are 8955 words starting with the letter r There are 22759 words starting with the letter s There are 11389 words starting with the letter t There are 16179 words starting with the letter u There are 3079 words starting with the letter v There are 3607 words starting with the letter w There are 293 words starting with the letter x There are 532 words starting with the letter y There are 719 words starting with the letter z
ตัวอย่างด้านบนพิมพ์ผลลัพธ์แบบเหมาะให้คนอ่าน แต่อาจนำไปใช้ในโปรแกรมอื่นๆยาก ถ้าเราจะให้ผลลัพธ์ไปใช้ในโปรแกรมอื่นๆสะดวกเราก็ควรเปลี่ยน print(f"There are {count} words starting with the letter {letter}")
ให้เป็นรูปแบบที่อ่านง่ายด้วยโปรแกรมเช่น print(f"{letter}:{count}")
หรือ print(f"{letter}\t{count}")
inputfile = open('/usr/share/dict/words')
count = {}
lowercase_letters = "abcdefghijklmnopqrstuvwxyz"
for letter in lowercase_letters:
count[letter] = 0
for word in inputfile:
word = word.strip()
for letter in lowercase_letters:
if word.startswith(letter):
count[letter] += 1
for letter, count in count.items():
#print(f"{letter}:{count}") #แบ่งด้วย :
print(f"{letter}\t{count}") #แบ่งด้วย tab (\t)
a 14537 b 9675 c 17406 d 9946 e 7818 f 6382 g 5843 h 7889 i 8303 j 1158 k 1735 l 5211 m 10709 n 6098 o 7219 p 22171 q 1075 r 8955 s 22759 t 11389 u 16179 v 3079 w 3607 x 293 y 532 z 719