Quiz 7: Files | Python for Everybody 配套练习_解题记录


课程简介

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)

  1. Given the architecture and terminology we introduced in Chapter 1, where are files stored?
  • Motherboard
  • Secondary memory
  • Main Memory
  • Machine Language
  1. 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
  1. 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
  1. What Python function would you use if you wanted to prompt the user for a file name to open?
  • alert()
  • file_input()
  • gets()
  • input()
  1. 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
  1. 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:
  1. 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
  1. 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()
  1. 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
  1. 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 the sum() 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 enter mbox-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)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冰.封万里

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值