Python break statement

Python break statements, like in C, break the minimal closed for or while loop.

The break statement is used to terminate the loop statement. That is, if the loop condition has no False condition or the sequence has not been completely recursed, the loop statement will be stopped.

The break statement is used in the while and for loops.

If you use nested loops, the break statement will stop executing the deepest loop and start executing the next line of code.

Python language break statement syntax:

break

Flowchart:

Instance(Python 2.0+)

#!/usr/bin/python # -*- coding: UTF-8 -*- for letter in 'Python': # First instance if letter == 'h': break print 'Current letter :', letter var = 10 # Second instance while var > 0: print 'Current variable value :', var var = var -1 if var == 5: # Exit the loop when the variable var is equal to 5 break print "Good bye!"

The above example execution results´╝Ü

Current letter : P
Current letter : y
Current letter : t
Current variable value : 10
Current variable value : 9
Current variable value : 8
Current variable value : 7
Current variable value : 6
Good bye!

movies.xml

<collection shelf="New Arrivals"> <movie title="Enemy Behind"> <type>War, Thriller</type> <format>DVD</format> <year>2003</year> <rating>PG</rating> <stars>10</stars> <description> Talk about a US-Japan war</description> </movie> <movie title="Transformers"> <type>Anime, Science Fiction</type> <format>DVD</format> <year>1989</year> <rating>R</rating> <stars>8</stars> <description>A schientific fiction</description> </movie> <movie title="Trigun"> <type>Anime, Action</type> <format>DVD</format> <episodes>4</episodes> <rating>PG</rating> <stars>10</stars> <description>Vash the Stampede!</description> </movie> <movie title="Ishtar"> <type>Comedy</type> <format>VHS</format> <rating>PG</rating> <stars>2</stars> <description>Viewable boredom</description> </movie> </collection>

python uses SAX to parse xml

SAX is an event-driven API.

Parsing XML documents with SAX involves two parts: parser and event handler.

The parser is responsible for reading the XML document and sending events to the event handler, such as an element start with an element end event.

The event handler is responsible for responding to events and processing the passed XML data.

sax is suitable for handling the following questions:

  • 1. Processing large files;
  • 2. Only need part of the file, or just get specific information from the file.
  • 3. When you want to build your own object model.

In Python, using sax to process xml first introduces the parse function in xml.sax, and the ContentHandler in xml.sax.handler.

Introduction to the ContentHandler class method

characters(content)method

Call timing:

From the beginning of the line, there are characters before the label is encountered, and the value of content is the string.

From a tag, before the next tag is encountered, there are characters, and the value of content is the string.

From a label, before the line terminator is encountered, there are characters, and the value of content is the string.

The

tag can be either a start tag or an end tag.

startDocument() method

Called when the document is launched.

endDocument() method

Called when the parser reaches the end of the document.

startElement(name, attrs) method

Invoked when the XML start tag is encountered, name is the name of the tag, and attrs is the tag's attribute value dictionary.

endElement(name) method

Called when an XML end tag is encountered.


make_parser method

The following method creates a new parser object and returns.

xml.sax.make_parser( < /span>[parser_list] )

Parameter description:

  • parser_list - Optional parameters, list of parsers

parser method

The following method creates a SAX parser and parses the xml document:

xml.sax.parse( xmlfile , contenthandler[, errorhandler])

Parameter description:

  • xmlfile - Xml file name
  • contenthandler - Must be a ContentHandler object
  • errorhandler - If this parameter is specified, the errorhandler must be a SAX ErrorHandler object

parseString method

The parseString method creates an XML parser and parses the xml string:

xml.sax.parseString(xmlstring , contenthandler[, errorhandler])

Parameter description:

  • xmlstring - Xml string
  • contenthandler - Must be a ContentHandler object
  • errorhandler - If this parameter is specified, the errorhandler must be a SAX ErrorHandler object

Python parses XML instances

Instance

#!/usr/bin/python # -*- coding: UTF-8 -*- import xml.sax class MovieHandler( xml.sax.ContentHandler ): def __init__(self): self.CurrentData = "" self.type = "" self.format = "" self.year = "" self.rating = "" self.stars = "" self.description = "" # Element start event processing def startElement(self, tag, attributes): self.CurrentData = tag if tag == "movie": print "*****Movie*****" title = attributes["title"] print "Title:", title
# Element end event processing def endElement(self, tag): if self.CurrentData == "type": print "Type:", self.type elif self.CurrentData == "format": print "Format:", self.format elif self.CurrentData == "year": print "Year:", self.year elif self.CurrentData == "rating": print "Rating:", self.rating elif self.CurrentData == "stars": print "Stars:", self.stars elif self.CurrentData == "description": print "Description:", self.description self.CurrentData = "" # Content event processing def characters(self, content): if self.CurrentData == "type": self.type = content elif self.CurrentData == "format": self.format = content elif self.CurrentData == "year": self.year = content elif self.CurrentData == "rating": self.rating = content elif self.CurrentData == "stars": self.stars = content elif self.CurrentData == "description": self.description = content if ( __name__ == "__main__"): # Create XMLReader parser = xml.sax.make_parser() # turn off namepsaces parser.setFeature(xml.sax.handler.feature_namespaces, 0) # Rewrite ContextHandler Handler = MovieHandler() parser.setContentHandler( Handler ) parser.parse("movies.xml")





welookups is optimized for learning.© welookups .
All Right Reserved and you agree to have read and accepted our term and condition