Danh mục

Dive Into Python-Chapter 10. Scripts and Streams

Số trang: 49      Loại file: pdf      Dung lượng: 183.47 KB      Lượt xem: 10      Lượt tải: 0    
Thư Viện Số

Hỗ trợ phí lưu trữ khi tải xuống: 6,000 VND Tải xuống file đầy đủ (49 trang) 0

Báo xấu

Xem trước 5 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Tham khảo tài liệu dive into python-chapter 10. scripts and streams, công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
Dive Into Python-Chapter 10. Scripts and Streams Chapter 10. Scripts and Streams10.1. Abstracting input sourcesOne of Pythons greatest strengths is its dynamic binding, and one powerfuluse of dynamic binding is the file-like object.Many functions which require an input source could simply take a filename,go open the file for reading, read it, and close it when theyre done. But theydont. Instead, they take a file-like object.In the simplest case, a file-like object is any object with a read method withan optional size parameter, which returns a string. When called with no sizeparameter, it reads everything there is to read from the input source andreturns all the data as a single string. When called with a size parameter, itreads that much from the input source and returns that much data; whencalled again, it picks up where it left off and returns the next chunk of data.This is how reading from real files works; the difference is that youre notlimiting yourself to real files. The input source could be anything: a file ondisk, a web page, even a hard-coded string. As long as you pass a file-likeobject to the function, and the function simply calls the objects read method,the function can handle any kind of input source without specific code tohandle each kind.In case you were wondering how this relates to XML processing,minidom.parse is one such function which can take a file-like object.Example 10.1. Parsing XML from a file>>> from xml.dom import minidom>>> fsock = open(binary.xml) 1>>> xmldoc = minidom.parse(fsock) 2>>> fsock.close() 3>>> print xmldoc.toxml() 4 0 1 \1 First, you open the file on disk. This gives you a file object.2 You pass the file object to minidom.parse, which calls the readmethod of fsock and reads the XML document from the file on disk.3 Be sure to call the close method of the file object after youre donewith it. minidom.parse will not do this for you.4 Calling the toxml() method on the returned XML document prints outthe entire thing.Well, that all seems like a colossal waste of time. After all, youve alreadyseen that minidom.parse can simply take the filename and do all the openingand closing nonsense automatically. And its true that if you know youre justgoing to be parsing a local file, you can pass the filename andminidom.parse is smart enough to Do The Right Thing™. But notice howsimilar -- and easy -- it is to parse an XML document straight from theInternet.Example 10.2. Parsing XML from a URL>>> import urllib>>> usock = urllib.urlopen(http://slashdot.org/slashdot.rdf) 1>>> xmldoc = minidom.parse(usock) 2>>> usock.close() 3>>> print xmldoc.toxml() 4Slashdothttp://slashdot.org/News for nerds, stuff that mattersSlashdothttp://images.slashdot.org/topics/topicslashdot.gifhttp://slashdot.org/To HDTV or Not to HDTV?http://slashdot.org/article.pl?sid=01/12/28/0421241[...snip...]1 As you saw in a previous chapter, urlopen takes a web page URL andreturns a file-like object. Most importantly, this object has a read methodwhich returns the HTML source of the web page.2 Now you pass the file-like object to minidom.parse, which obedientlycalls the read method of the object and parses the XML data that the readmethod returns. The fact that this XML data is now coming straight from aweb page is completely irrelevant. minidom.parse doesnt know about webpages, and it doesnt care about web pages; it just knows about file-likeobjects.3 As soon as youre done with it, be sure to close the file-like object thaturlopen gives you.4 By the way, this URL is real, and it really is XML. Its an XMLrepresentation of the current headlines on Slashdot, a technical news andgossip site.Example 10.3. Parsing XML from a string (the easy but inflexible way)>>> contents = 01>>> xmldoc = minidom.parseString(contents) 1>>> print xmldoc.toxml()011 minidom has a method, parseString, which takes an entire XMLdocument as a string and parses it. You can use this instead ofminidom.parse if you know you already have your entire XML document ina string.OK, so you can use the minidom.parse function for parsing both local filesand remote URLs, but for parsing strings, you use... a different function.That means that if you want to be able to take input from a file, a URL, or astring, youll need special logic to check whether its a string, and call theparseString function instead. How unsatisfying.If there were a way to turn a string into a file-like object, then you couldsimply pass this object to minidom.parse. And in fact, there is a modulespecifically designed for doing just that: StringIO.Example 10.4. Introducing StringIO>>> contents = 01>>> import StringIO>>> ssock = StringIO.StringIO(contents) 1>>> ssock.read() 201>>> ssock.read() 3>>> ssock.seek(0) 4>>> ssock.read(15) 51>>> ssock.close() 61 The StringIO module contains a single class, also called StringIO,which allows you to turn a string into a file-like object. The StringIO classtakes the string as a parameter when creating an instance.2 Now you have a file-like object, and you can do all sorts of file-likethings with it. Like read, which returns the original string.3 Calling read again returns an empty string. This is how real fileobjects work too; once you read the entire file, you cant read any mo ...

Tài liệu được xem nhiều: