1. 程式人生 > >PR4E第七週作業

PR4E第七週作業

本週講了基本的文字檔案操作

首先是開啟檔案的函式open(),值得注意的是open()返回的不是全部檔案內容(記憶體可能吃不下),而是操作連結記憶體與因硬碟檔案的handle,起到一個connection的作用

然後文字檔案中每一行字元的末尾是換行字元“/n”(這是一個字元),注意print函式執行完也會新增一個“/n”字元,可能會有重複換行的問題。當遇到此問題要用strip()函式清楚字元首尾的“/n”字元

fh = open(words.txt)
for line in fh
用for迴圈函式用“line”可以逐行讀取檔案內容
inp = fh.read()

可以將檔案內容完全讀入一個字串中

 可以用if string.startswith()篩選或者過濾特定行。

用raw_input()輸入檔名。

本次有兩個作業,分別是

7.1 Write a program that prompts for a file name, then opens that file and reads through the file, and print the contents of the file in upper case. Use the filewords.txt to produce the output below.

答案:
# Use words.txt as the file name
fname = raw_input("Enter file name: ")
fh = open(fname)
inp = fh.read()
inp = inp.upper()
inp = inp.rstrip()


print inp
7.2 Write a program that prompts for a file name, then opens that file and reads through the file, looking for lines of the form:
X-DSPAM-Confidence:    0.8475
Count these lines and extract the floating point values from each of the lines and compute the average of those values and produce an output as shown below.

You can download the sample data at http://www.pythonlearn.com/code/mbox-short.txtwhen you are testing below enter mbox-short.txt as the file name.

答案

# Use the file name mbox-short.txt as the file name
fname = raw_input("Enter file name: ")
fh = open(fname)
count = 0
sum = 0
ave = 0
for line in fh:
    if not line.startswith("X-DSPAM-Confidence:") : continue
    space = line.find(" ")
    raw_num = line[space:]
    num = raw_num.strip()
    num = num.strip()
    num = float(num)
    count = count+1
    sum = sum+num

ave = sum/count
print "Average spam confidence:",ave

其中第二個答案忘記了find函式,一開始是數出space字元的位置然後直接領line = line[20:]的,囧