PY4E课程官网:https://www.py4e.com/
参考文章:题目及答案汇总(Github)
Assignment 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 file words.txt to produce the output below.You can download the sample data at http://www.pythonlearn.com/code/words.txt
编写一个程序,提示输入文件名,然后打开该文件并读取该文件,并打印大写的文件内容。使用文件word.txt来产生下面的输出。你可以在 http://www.pythonlearn.com/code/words.txt 下载样本数据。
import sys
fname = input('Enter the file name: ')
try:
fhand = open(fname)
except:
print(fname, 'File does not exist ')
sys.exit()
inp = fhand.read()
print(inp.upper())
>>>Enter the file name:test.txt
test.txt File does not exist
An exception has occurred, use %tb to see the full traceback.
>>>Enter the file name: words.txt
WRITING PROGRAMS OR PROGRAMMING IS A VERY CREATIVE
AND REWARDING ACTIVITY YOU CAN WRITE PROGRAMS FOR
MANY REASONS RANGING FROM MAKING YOUR LIVING TO SOLVING
A DIFFICULT DATA ANALYSIS PROBLEM TO HAVING FUN TO HELPING
SOMEONE ELSE SOLVE A PROBLEM THIS BOOK ASSUMES THAT
{\EM EVERYONE} NEEDS TO KNOW HOW TO PROGRAM AND THAT ONCE
YOU KNOW HOW TO PROGRAM, YOU WILL FIGURE OUT WHAT YOU WANT
TO DO WITH YOUR NEWFOUND SKILLS
WE ARE SURROUNDED IN OUR DAILY LIVES WITH COMPUTERS RANGING
FROM LAPTOPS TO CELL PHONES WE CAN THINK OF THESE COMPUTERS
AS OUR PERSONAL ASSISTANTS WHO CAN TAKE CARE OF MANY THINGS
ON OUR BEHALF THE HARDWARE IN OUR CURRENT-DAY COMPUTERS
IS ESSENTIALLY BUILT TO CONTINUOUSLY ASK US THE QUESTION
WHAT WOULD YOU LIKE ME TO DO NEXT
OUR COMPUTERS ARE FAST AND HAVE VASTS AMOUNTS OF MEMORY AND
COULD BE VERY HELPFUL TO US IF WE ONLY KNEW THE LANGUAGE TO
SPEAK TO EXPLAIN TO THE COMPUTER WHAT WE WOULD LIKE IT TO
DO NEXT IF WE KNEW THIS LANGUAGE WE COULD TELL THE
COMPUTER TO DO TASKS ON OUR BEHALF THAT WERE REPTITIVE
INTERESTINGLY, THE KINDS OF THINGS COMPUTERS CAN DO BEST
ARE OFTEN THE KINDS OF THINGS THAT WE HUMANS FIND BORING
AND MIND-NUMBING
Assignment 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.txt when
you are testing below enter mbox-short.txt as the file name.
编写一个程序,提示输入文件名,然后打开该文件并读取文件,寻找表格中的行。
X-DSPAM-Confidence: 0.8475
对这些行进行计数,并从每一行中提取浮点值,计算这些值的平均值,并产生如下图所示的输出。你可以在http://www.pythonlearn.com/code/mbox-short.txt 下载样本数据,当你在下面测试时输入mbox-short.txt作为文件名。
import sys
fname = input('Enter the file name: ')
try:
fhand = open(fname)
except:
print(fname, 'File does not exist ')
sys.exit()
count = sum = ave= 0
for line in fhand:
if line.startswith("X-DSPAM-Confidence:"):
count = count + 1
colon_index = line.find(':')
# make the string only remain whitespaces and the number
value_str = line[colon_index + 1:]
# strip whitespaces and type transition
value = float(value_str.strip())
sum += value
ave = sum/count
print('Count is:', count, " Sum is:%.3f" % sum, " Average is:%.3f" % ave)
>>>Enter the file name: test.txt
test.txt File does not exist
An exception has occurred, use %tb to see the full traceback.
>>>Enter the file name: mbox-short.txt
Count is: 27 Sum is:20.269 Average is:0.751