7: String Processing

7-4: Reading & writing Files

** ถ้าใช้งานบนมือถือหรือ tablet แนะนำให้ใช้ Chrome หรือ Safari เท่านั้น **

แบบฝึกหัด 7-4 ข้อที่ 1

จงเขียนฟังก์ชัน count_line(file_name) เพือหาจำนวนบรรทัดของข้อมูลในแฟ้มชื่อ file_name

import io def open(f, mode='r'): if mode in ('r', 'rt'): if f == "4-lines.txt": return io.StringIO("1\n2\n3\n4\n") elif f == 'no-line.txt': return io.StringIO() else: raise FileNotFoundError(f) else: raise ValueError('invalid mode: ' + mode) def count_line(file_name): def count_line(file_name): fn = open(file_name) c = 0 for line in fn: c += 1 fn.close() return c q = "`" fname = "count_line" func = Ex().check_function_def(fname, missing_msg="ไม่พบฟังก์ชัน " + (q+fname+q)) func.has_equal_part_len('args', 'ฟังก์ชันนี้รับพารามิเตอร์หนึ่งตัว') for p in ["'no-line.txt'", "'4-lines.txt'"]: fcall = fname + "(" + p + ")" func.check_call(fcall).has_equal_value(incorrect_msg = (q+fcall.replace(" ", " ")+q) + " ให้ผลผิด")
open แฟ้ม และใช้วงวน for line in file นับจนกว่าจะออกจากวงวน
แบบฝึกหัด 7-4 ข้อที่ 2

จงเขียนฟังก์ชัน print_merge(filename1, filename2) อ่านข้อมูลของแต่ละบรรทัดจากทั้งสองแฟ้มมาสลับกันแสดงทางจอภาพ (เริ่มที่ file_name1) ถ้าแฟ้มใดหมด ก็อ่านอีกแฟ้มมาแสดงให้หมด

import io def open(f, mode='r'): if mode in ('r', 'rt'): if f == "A.txt": return io.StringIO("A\nB\nC") elif f == "B.txt": return io.StringIO("1\n2\n3\n4\n5") elif f == 'C.txt': return io.StringIO() else: raise FileNotFoundError(f) else: raise ValueError('invalid mode: ' + mode) def print_merge(filename1, filename2): def print_merge(filename1, filename2): f1 = open(filename1) f2 = open(filename2) d1 = f1.readline() d2 = f2.readline() while (len(d1) > 0 or len(d2) > 0): if len(d1) > 0: print(d1.strip()) d1 = f1.readline() if len(d2) > 0: print(d2.strip()) d2 = f2.readline() f1.close() f2.close() q = "`" fname = "print_merge" func = Ex().check_function_def(fname, missing_msg="ไม่พบฟังก์ชัน " + (q+fname+q)) func.has_equal_part_len('args', 'ฟังก์ชันนี้รับพารามิเตอร์สองตัว') for p in ["'A.txt', 'B.txt'", "'B.txt', 'A.txt'", "'C.txt', 'A.txt'", "'A.txt', 'C.txt'"]: fcall = fname + "(" + p + ")" func.check_call(fcall).has_equal_output(incorrect_msg = (q+fcall+q) + " ให้ผลผิด")
ใช้ while อ่านด้วย readline ถ้าอ่านมาได้สตริงความยาวศูนย์ ก็แสดงว่า แฟ้มนั้นอ่านจนหมดแล้ว
แบบฝึกหัด 7-4 ข้อที่ 3

จงเขียนฟังก์ชัน count_articles(filename) อ่านข้อความจากแฟ้มชื่อ filename เพื่อนับว่าในแฟ้มนี้มีคำว่า a, an และ the รวมทั้งหมดทุกบรรทัดในแฟ้มกี่ตัว

import io def open(f, mode='r'): if mode in ('r', 'rt'): d = ["(A, an and the).", "OK. they're 'A', 'An', 'The'", "Another an"] if f == "A.txt": return io.StringIO("\n".join(d)) elif f == 'no-line.txt': return io.StringIO() else: raise FileNotFoundError(f) else: raise ValueError('invalid mode: ' + mode) def replace_punctuation(s): # แทนเครื่องหมายวรรคตอนด้วยช่องว่าง t = "" for e in s: if e in "\"\'/\\,.:;()[]{}": t += " " else: t += e return t def count_word(words, w): # นับจำนวนคำ w ในลิสต์ของคำ words w = w.lower() c = 0 for e in words: if e.lower() == w: c += 1 return c def count_articles(filename): # นับจำนวน a, an และ the ของคำในแฟ้ม filename # def replace_punctuation(s): t = "" for e in s: if e in "\"\'/\\,.:;()[]{}": t += " " else: t += e return t def count_word(words, w): w = w.lower() c = 0 for e in words: if e.lower() == w: c += 1 return c def count_articles(filename): # นับจำนวน a, an และ the ของคำในแฟ้ม filename fn = open(filename) c = 0 for line in fn: words = replace_punctuation(line).split() c += count_word(words, "a") c += count_word(words, "an") c += count_word(words, "the") fn.close() return c q = "`" fname = "count_articles" func = Ex().check_function_def(fname, missing_msg="ไม่พบฟังก์ชัน " + (q+fname+q)) func.has_equal_part_len('args', 'ฟังก์ชันนี้รับพารามิเตอร์หนึ่งตัว') for p in ["'no-line.txt'", "'A.txt'"]: fcall = fname + "(" + p + ")" func.check_call(fcall).has_equal_value(incorrect_msg = (q+fcall+q) + " ให้ผลผิด")
ใช้ replace_punctuation แทนเครื่องหมายวรรคตอนด้วยช่องว่าง จากนั้น split แล้วไปนับคำที่สนใจด้วย count_word จะง่ายขึ้นเยอะเลย