文章目录
- Python for Everybody
- 课程简介
- Quiz 14_2-1: Using Web Services
- 单选题(1-15)
- 操作题
- Autograder : Extracting Data from XML
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 14_2-1: Using Web Services
Web services allow a program to access data available in a different server.
单选题(1-15)
- What is “serialization” when we are talking about web services?
- Marking each network packet so it can be put back into order on the receiving system
- Making it so that dictionaries can maintain their keys in sorted order
- Sorting all the data stored in a tuple
- The act of taking data stored in a program and formatting it so it can be sent across the network
- What is the name of the Python library to parse XML data?
- xml-misc
- xml.etree.ElementTree
- xml2
- xml.json
- Which of the following are not commonly used serialization formats?
- XML
- TCP
- HTTP
- Dictionaries
- JSON
- What is the method to cause Python to parse XML that is stored in a string?
- readall()
- fromstring()
- parse()
- xpath()
- extract()
- In this XML, which are the “complex elements”?
<people>
<person>
<name>Chuck</name>
<phone>303 4456</phone>
</person>
<person>
<name>Noah</name>
<phone>622 7421</phone>
</person>
</people>
- phone
- person
- name
- Noah
- people
- In this XML, which are the “simple elements”?
<people>
<person>
<name>Chuck</name>
<phone>303 4456</phone>
</person>
<<person>
<name>Noah</name>
<phone>622 7421</phone>
</person>
</people>
- phone
- name
- people
- person
- Noah
- In the following XML, which are attributes?
<person>
<name>Chuck</name>
<phone type="intl">
+1 734 303 4456
</phone>
<email hide="yes" />
</person>
- type
- person
- name
- hide
- In the following XML, which node is the parent node of node e
<a>
<b>X</b>
<c>
<d>Y</d>
<e>Z</e>
</c>
</a>
- c
- e
- a
- b
- Looking at the following XML, what text value would we find at path “/a/c/e”
<a>
<b>X</b>
<c>
<d>Y</d>
<e>Z</e>
</c>
</a>
- a
- Y
- Z
- e
- b
- What is the purpose of XML Schema?
- To transfer XML data reliably during network outages
- To establish a contract as to what is valid XML
- A Python program to tranform XML files
- To compute SHA1 checksums on data to make sure it is not modified in transit
- If you were building an XML Schema and wanted to limit the values allowed in an xs:string field to only those in a particular list, what XML tag would you use in your XML Schema definition?
- xs:sequence
- xs:enumeration
- xs:complexType
- xs:element
- maxOccurs
- For this XML Schema:
<xs:complexType name=”person”>
<xs:sequence>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
<xs:element name="dateborn" type="xs:date"/>
</xs:sequence>
</xs:complexType>
And this XML,
<person>
<lastname>Severance</lastname>
<Age>17</Age>
<dateborn>2001-04-17</dateborn>
</person>
Which tag is incorrect?
- lastname
- person
- age
- Age
- dateborn
- What does the “Z” mean in this representation of a time:
2002-05-30T09:30:10Z
- The local timezone for this time is New Zealand
- This time is in the UTC timezone
- The hours value is in the range 0-12
- This time is Daylight Savings Time
- What is a good time zone to use when computers are exchanging data over APIs?
- Universal Time / GMT
- The local time zone of the sending computer
- The local time zone of the receiving computer
- The local time zone of the sending computer without daylight savings time
- Which of the following dates is in ISO8601 format?
- 05/30/2002
- 2002-05-30T09:30:10Z
- May 30, 2002
- 2002-May-30
操作题
Autograder : Extracting Data from XML
Extracting Data from XML
In this assignment you will write a Python program somewhat similar to http://www.py4e.com/code3/geoxml.py. The program will prompt for a URL, read the XML data from that URL using urllib and then parse and extract the comment counts from the XML data, compute the sum of the numbers in the file.
We provide two files for this assignment. One is a sample file where we give you the sum for your testing and the other is the actual data you need to process for the assignment.
- Sample data: http://py4e-data.dr-chuck.net/comments_42.xml
(Sum=2553) - Actual data: http://py4e-data.dr-chuck.net/comments_1577745.xml
(Sum ends with 90)
You do not need to save these files to your folder since your program will read the data directly from the URL. Note: Each student will have a distinct data url for the assignment - so only use your own data url for analysis.
Data Format and Approach
The data consists of a number of names and comment counts in XML as follows:
<comment>
<name>Matthias</name>
<count>97</count>
</comment>
You are to look through all the <comment>
tags and find the <count>
values sum the numbers. The closest sample code that shows how to parse XML is geoxml.py. But since the nesting of the elements in our data is different than the data we are parsing in that sample code you will have to make real changes to the code.
To make the code a little simpler, you can use an XPath selector string to look through the entire tree of XML for any tag named ‘count’ with the following line of code:
counts = tree.findall('.//count')
Take a look at the Python ElementTree documentation and look for the supported XPath syntax for details. You could also work from the top of the XML down to the comments node and then loop through the child nodes of the comments node.
Sample Execution
$ python3 solution.py
Enter location: http://py4e-data.dr-chuck.net/comments_42.xml
Retrieving http://py4e-data.dr-chuck.net/comments_42.xml
Retrieved 4189 characters
Count: 50
Sum: 2...