文章目录
- 课程简介
- Quiz 7: Files
- 单选题(1-10)
- 编程题
- Exercise 7.2
课程简介
Python for Everybody 零基础程序设计(Python 入门)
- This course aims to teach everyone the basics of programming computers using Python. 本课程旨在向所有人传授使用 Python 进行计算机编程的基础知识。
- We cover the basics of how one constructs a program from a series of simple instructions in Python. 我们介绍了如何通过 Python 中的一系列简单指令构建程序的基础知识。
- The course has no pre-requisites and avoids all but the simplest mathematics. Anyone with moderate computer experience should be able to master the materials in this course. 该课程没有任何先决条件,除了最简单的数学之外,避免了所有内容。任何具有中等计算机经验的人都应该能够掌握本课程中的材料。
- This course will cover Chapters 1-5 of the textbook “Python for Everybody”. Once a student completes this course, they will be ready to take more advanced programming courses. 本课程将涵盖《Python for Everyday》教科书的第 1-5 章。学生完成本课程后,他们将准备好学习更高级的编程课程。
- This course covers Python 3.
coursera
Python for Everybody 零基础程序设计(Python 入门)
Charles Russell Severance
Clinical Professor
个人主页
Twitter
University of Michigan
课程资源
coursera原版课程视频
coursera原版视频-中英文精校字幕-B站
Dr. Chuck官方翻录版视频-机器翻译字幕-B站
PY4E-课程配套练习
Dr. Chuck Online - 系列课程开源官网
Quiz 7: Files
We learn how to open data files on your computer and read through the files using Python.
单选题(1-10)
- Given the architecture and terminology we introduced in Chapter 1, where are files stored?
- Motherboard
- Secondary memory
- Main Memory
- Machine Language
- What is stored in a “file handle” that is returned from a successful open() call?
- The handle contains the first 10 lines of a file
- The handle has a list of all of the files in a particular folder on the hard drive
- The handle is a connection to the file’s data
- All the data from the file is read into memory and stored in the handle
- What do we use the second parameter of the open() call to indicate?
- How large we expect the file to be
- Whether we want to read data from the file or write data to the file
- The list of folders to be searched to find the file we want to open
- What disk drive the file is stored on
- What Python function would you use if you wanted to prompt the user for a file name to open?
- alert()
- file_input()
- gets()
- input()
- What is the purpose of the newline character in text files?
- It allows us to open more than one files and read them in a synchronized manner
- It adds a new network connection to retrieve files from the network
- It indicates the end of one line of text and the beginning of another line of text
- It enables random movement throughout the file
- If we open a file as follows:
xfile = open('mbox.txt')
- [ ]What statement would we use to read the file one line at a time?
- while line = xfile.gets
- while (
<xfile>
) { - while ((line = xfile.readLine()) != null) {
- for line in xfile:
- What is the purpose of the following Python code?
fhand = open('mbox.txt')
x = 0
for line in fhand:
x = x + 1
print(x)
- Count the lines in the file ‘mbox.txt’
- Remove the leading and trailing spaces from each line in mbox.txt
- Reverse the order of the lines in mbox.txt
- Convert the lines in mbox.txt to lower case
- If you write a Python program to read a text file and you see extra blank lines in the output that are not present in the file input as shown below, what Python string function will likely solve the problem?
From: stephen.marquard@uct.ac.za
From: louis@media.berkeley.edu
From: zqian@umich.edu
From: rjlowe@iupui.edu
- rstrip()
- startswith()
- trim()
- ljust()
- The following code sequence fails with a traceback when the user enters a file that does not exist. How would you avoid the traceback and make it so you could print out your own error message when a bad file name was entered?
fname = input('Enter the file name: ')
fhand = open(fname)
- try / catch / finally
- setjmp / longjmp
- try / except
- signal handlers
- What does the following Python code do?
fhand = open('mbox-short.txt')
inp = fhand.read()
- Prompts the user for a file name
- Reads the entire file into the variable inp as a string
- Turns the text in the file into a graphic image like a PNG or JPG
- Checks to see if the file exists and can be written
编程题
Exercise 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.
Do not use thesum()
function or a variable named sum in your solution.
You can download the sample data at http://www.py4e.com/code3/mbox-short.txt when you are testing below entermbox-short.txt
as the file name.
[Desired Output]
Average spam confidence: 0.7507185185185187
解题代码:
# Use the file name mbox-short.txt as the file name fname = input("Enter file name: ") fh = open(fname) count = 0 xc = 0 for line in fh: if not line.startswith("X-DSPAM-Confidence:"): continue else: n = line.find(':') + 1 count += 1 xc += float(line[n:]) print("Average spam confidence:", xc / count)