0% found this document useful (0 votes)
12 views

UNIT4

Uploaded by

parinlimbad6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

UNIT4

Uploaded by

parinlimbad6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 67

Unit - 4

Prepared By : Radadiya Jitendra 1


Regular Expressions
 A regular expression is a special sequence of characters that helps you
match or find other strings or sets of strings, using a specialized syntax held
in a pattern.

 It is extremely useful for extracting information from text such as code,


files, log, spreadsheets or even documents.

 The module re provides full support for regular expressions in Python.

 In Python, a regular expression is denoted as RE are imported through re


module.
 In Python regular expression supports various things like Modifiers,
Identifiers, and White space characters.

 The re module raises the exception re.error if an error occurs while


compiling or using a regular expression.

Prepared By : Radadiya Jitendra 2


Regular Expressions
 We would all important functions, which would be used to
handle regular expressions.
 There are various characters, which would have special
meaning when they are used in regular expression.
Regular Expression Syntax
import re
 "re" module included with Python primarily used for string
searching and manipulation
 Also used frequently for web page "Scraping" (extract large
amount of data from websites).

Prepared By : Radadiya Jitendra 3


Regular Expressions
 There are five functions, which would be used to handle
regular expressions.
1. Match() function : The match() function is used to match the RE
pattern to string.
2. Search() function : The search() function is used to search the RE
pattern from entire string.
3. Findall() function : The findall() function is used to find the RE
pattern from entire string.
4. Split() function : The split() function is used to split string from the
RE pattern.
5. Sub() function : The sub() function is used to replace string.

Prepared By : Radadiya Jitendra 4


The match Function
 This function attempts to match RE pattern to string with
optional flags.
syntax for this function
re.match(pattern, string, flag)
 Pattern : This is the regular expression to be matched.
 string : This is the string, which would be searched to match the
pattern at the beginning of string.
 flag : You can specify different flags using bitwise OR (|). These are
modifiers, re.I (ignore case) and re.M(Multiline).

Prepared By : Radadiya Jitendra 5


The match Function
Example :
import re
txt = "The rain in India"
x = re.match(“The”, txt)
if (x):
print("YES! We have a match!")
else:
print("No match")
Output :
YES! We have a match!
Prepared By : Radadiya Jitendra 6
The match Function
Example :
import re
txt = "The rain in India"
x = re.match(“rain”, txt)
if (x):
print("YES! We have a match!")
else:
print("No match")
Output :
No match
Prepared By : Radadiya Jitendra 7
The search Function
 This function searches for first occurrence of RE pattern within
string with optional flags.
syntax for this function −
re.search(pattern, string, flag)
 Pattern : This is the regular expression to be matched.
 string : This is the string, which would be searched to match the
pattern anywhere in the string.
 flag : You can specify different flags using bitwise OR (|). These are
modifiers, re.I (ignore case) and re.M(Multiline).

Prepared By : Radadiya Jitendra 8


The search Function
Example
import re
txt = "The rain in India"
x = re.search(“The”, txt)
if (x):
print("YES! We have a match!")
else:
print("No match")
Output :
YES! We have a match!
Prepared By : Radadiya Jitendra 9
The search Function
Example
import re
txt = "The rain in India"
x = re.search(“rain”, txt)
if (x):
print("YES! We have a match!")
else:
print("No match")
Output :
YES! We have a match!
Prepared By : Radadiya Jitendra 10
match vs search Function
 Python offers two different primitive operations based on
regular expressions:
 match checks for a match only at the beginning of the
string.
 while search checks for a match anywhere in the string.

Prepared By : Radadiya Jitendra 11


The findall Function
 The findall() function returns a list containing all matches.

syntax for this function −

re.findall(pattern, string, flag)

 Pattern : This is the regular expression to be matched.

 string : This is the string, which would be searched to match the


pattern anywhere in the string.
 flag : You can specify different flags using bitwise OR (|). These are
modifiers, re.I (ignore case) and re.M(Multiline).

Prepared By : Radadiya Jitendra 12


The findall Function
Example
import re
str = "The rain in India"
x = re.findall(“i", str)
print(x)

Output
['i', 'i', 'i']

Prepared By : Radadiya Jitendra 13


The findall Function
Example
import re
str = "The rain in India"
x = re.findall(“i", str, re.I)
print(x)

Output
['i', 'i', ‘I’, 'i']

Prepared By : Radadiya Jitendra 14


The split Function
 The split() function returns a list where the string has been
split at each match:
 syntax for this function −
re.split(pattern, string, flag)
 Pattern : This is the regular expression to be matched.
 string : This is the string, which would be searched to match the
pattern anywhere in the string.
 Flag : You can specify different flags using bitwise OR (|). These are
modifiers, re.I (ignore case) and re.M(Multiline).

Prepared By : Radadiya Jitendra 15


The split Function
Example
import re
str = "The rain in India"
x = re.split(“i", str)
print(‘ '.join(x))
Output
The ra n n Ind a

Prepared By : Radadiya Jitendra 16


The sub Function
 It helps to search a pattern and replace with a new sub string.
If the pattern is not found, string is returned unchanged.
 syntax for this function −
re.sub(pattern, repl , string,max)
 Pattern : This is the regular expression to be matched.
 repl : Returns the string obtained by replacing the leftmost non-
overlapping occurrences of the RE in string by the
replacement replacement.
 string : This is the string, which would be searched to match the
pattern anywhere in the string.
 Max=no of occurrence (replecement).
Prepared By : Radadiya Jitendra 17
The sub Function
Example
import re
str = "The rain in India"
x = re.sub(“ ",“_", str)
print(x)

Output
The_rain_in_India

Prepared By : Radadiya Jitendra 18


The sub Function
Example
import re
str = "The rain in India"
x = re.sub(“ ",“_", str,2)
print(x)

Output
The_rain_in India

Prepared By : Radadiya Jitendra 19


Sets
 A set is a set of characters inside a pair of square
brackets [] with a special meaning:
[arn] Returns a match where one of the specified characters (a, r, or n) are
 \Signals a special
presentsequence (can also be used to escape special
characters)"\d"Try
[a-n]
it ».Any character (except newline
Returns a match for any lower case character, alphabetically
character)"he..o"Try it »^Starts
between a and n with"^hello"Try it »$Ends
with"world$"Try it »*Zero or more occurrences"aix*"Try it »+One or
[^arn] Returns a match for any character EXCEPT a, r, and n
more occurrences"aix+"Try it »{}Excactly the specified number of
occurrences"al{2}"Try
[0123] it »|Either
Returns a match where anyor
of the specified digits (0, 1, 2, or 3) are
present

[0-9] Returns a match for any digit between 0 and 9

[0-5][0-9] Returns a match for any two-digit numbers from 00 and 59

[a-zA-Z] Returns a match for any character alphabetically between a and z, lower
case OR upper case
Prepared By : Radadiya Jitendra 20
Sets
Example
import re
str = "Welcome 2 the World of Python"
x = re.findall("[a-m]",str)
print(' '.join(x))

Output
elcmeheldfh

Prepared By : Radadiya Jitendra 21


Sets
Example
import re
str = "Welcome 2 the World of Python"
x = re.findall("[^a-m]",str)
print(' '.join(x))

Output
Wo 2 t Wor o Pyton

Prepared By : Radadiya Jitendra 22


Python expression Patterns
Character classes
[Pp]ython : Match "Python" or "python"
rub[ye] : Match "ruby" or "rube"
[aeiou] : Match any one lowercase vowel
[0-9] : Match any digit; same as [0123456789]
[a-z] : Match any lowercase ASCII letter
[A-Z] : Match any uppercase ASCII letter
[a-zA-Z0-9] : Match any of the above
[^aeiou] : Match anything other than a lowercase vowel
[^0-9] : Match anything other than a digit
Prepared By : Radadiya Jitendra
23
Python expression Patterns
Special Character Classes
. : Match any character except newline
\d : Match a digit: [0-9]
\D : Match a nondigit: [^0-9]
\s : Match a whitespace character: [ \t\r\n\f]
\S : Match nonwhitespace: [^ \t\r\n\f]
\w : Match a single word character: [A-Za-z0-9_]
\W : Match a nonword character: [^A-Za-z0-9_]

Prepared By : Radadiya Jitendra 24


Python expression Patterns
Repetition Cases
ruby? : Match "rub" or "ruby": the y is optional
ruby* : Match "rub" plus 0 or more
ruby+ : Match "rub" plus 1 or more
\d{3} : Match exactly 3 digits
\d{3,} : Match 3 or more digits
\d{3,5}: Match 3, 4, or 5 digits

Prepared By : Radadiya Jitendra 25


Sets
Find all digit characters
Example
import re
str = "Welcome 2 the World of Python"
x = re.findall(“\d",str)
print(' '.join(x))

Output
2

Prepared By : Radadiya Jitendra 26


Sets
Search for a sequence that starts with "W", followed by two (any)
characters:
Example
import re
str = "Welcome 2 the World of Python"
x = re.findall("W..", str)
print(' '.join(x))

Output
Wel Wor
Prepared By : Radadiya Jitendra 27
Sets
Check if the string starts with 'Welcome‘
import re
str = "Welcome 2 the World of Python"
x = re.findall('^Wel', str)
if (x):
print("Yes, the string starts with 'Welcome'")
else:
print("No match")
Output
Yes, the string starts with 'Welcome'
Prepared By : Radadiya Jitendra 28
Sets
Check if the string contains "or" followed by 0 or more "x"
characters
import re
str = "Welcome 2 the World of Python"
x = re.findall("or*", str)
print(' '.join(x))
Output
o or o o

Prepared By : Radadiya Jitendra 29


Text Processing
 Python Programming can be used to process text data for the
requirements in various textual data analysis.
 A very important area of application of such text processing
ability of python is for NLP (Natural Language Processing).
 Text processing has a direct application to Natural Language
Processing, also known as NLP.
 NLP is aimed at processing the languages spoken or written by
humans when they communicate with one another.
 NLP tries to understand the natural language spoken by
humans and classify it, analyses it as well if required respond to it.
 Regardless of what type of applications you create, you will
need to process human‐readable data, which is referred to
generally as text. Prepared By : Radadiya Jitendra 30
Text Processing
 Python’s standard library provides three text processing
modules and packages : csv, json and xml.
Comma Seperated Values (CSV) :
 CSV (Comma Separated Values) is a simple file format used to
store tabular data, such as a spreadsheet or database.
 A CSV file stores tabular data (numbers and text) in plain text.
Each line of the file is a data record.
 Each record consists of one or more fields, separated by
commas. The use of the comma as a field separator is the source
of the name for this file format.
 For working CSV files in python, there is an inbuilt module
called csv.
Prepared By : Radadiya Jitendra 31
CSV Module
Working with the CSV Module
 To pull information from CSV files you use loop and split
methods to get the data from individual columns.
 The CSV module explicitly exists to handle this task, making it
much easier to deal with CSV formatted files.
 This becomes especially important when you are working with
data that’s been exported from actual spreadsheets and databases
to text files.
 This information can be tough to read on its own.
 Unfortunately, there is no standard so the CSV module uses
“dialects” to support parsing using different parameters.
 Along with a generic reader and writer, the module includes a
dialect for working with Microsoft
Prepared Excel
By : Radadiya and related files.
Jitendra 32
CSV Module
CSV Functions
 The CSV module includes all the necessary functions built in.
They are:
1. csv.reader
2. csv.writer
3. csv.register_dialect
4. csv.unregister_dialect
5. csv.get_dialect
6. csv.list_dialects
7. csv.field_size_limit

Prepared By : Radadiya Jitendra 33


CSV Module
Reading CSV Files
 To pull data from a CSV file, you must use the reader function
to generate a reader object.
 The reader function is designed to take each line of the file and
make a list of all columns.
 Then, you just choose the column you want the variable data
for. First create, std_DB1.txt file:

id,name,department,birthday month
1,Raj,BCA,November
2,Jay,BBA,March
3,Ajay,BCOM,June
Prepared By : Radadiya Jitendra 34
CSV Module
Example Reading CSV Files
import csv
with open('std_DB1.txt') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
if line_count == 0:
print("Column names are "," ".join(row))
line_count += 1
else:
print("\t",row[1]," study in the ",row[2]," department
and was born in ",row[3])
line_count += 1
print("Processed" ,line_count-1,"lines.")
Prepared By : Radadiya Jitendra 35
CSV Module
Output

Column names are id name department birthday month


Raj study in the BCA department and was born in November
Jay study in the BBA department and was born in March
Ajay study in the BCOM department and was born in June
Processed 3 lines.

Prepared By : Radadiya Jitendra 36


CSV Module
Reading CSV Files Into a Dictionary With csv
 Rather than deal with a list of individual String elements, you
can read CSV data directly into a dictionary (technically, an
Ordered Dictionary) as well.

Prepared By : Radadiya Jitendra 37


CSV Module
Example
import csv
with open('std_DB1.txt', mode='r') as csv_file:
csv_reader = csv.DictReader(csv_file)
line_count = 0
for row in csv_reader:
if line_count == 0:
print("Column names are "," ".join(row))
line_count += 1
else:
print("\t",row[1]," study in the ",row[2]," department
and was born in ",row[3])
line_count += 1
print("Processed" ,line_count-1,"lines.")
Prepared By : Radadiya Jitendra 38
CSV Module
Writing CSV Files With csv
 You can also write to a CSV file using a writer object and
writerow (to write the first row which is nothing but the field
names) & writerows(to write multiple rows at once).

Prepared By : Radadiya Jitendra 39


CSV Module
Example
import csv
fields = ['name', 'department', 'birthday month']
rows = [ [‘Sanjay', 'BCA', 'April'], [‘Kishan', 'BBA', 'September']]
filename = "std_DB2.csv“
with open(filename, 'w') as csvfile:
csvwriter = csv.writer(csvfile)
csvwriter.writerow(fields)
csvwriter.writerows(rows)
print("Successfully File Write")
Prepared By : Radadiya Jitendra 40
CSV Module
Example of Writing a dictionary to a CSV file
import csv
mydict =[{'branch': 'BCA', 'name': 'PYTHON', 'year': '3'},
{'branch': 'BBA', 'name': 'ACCOUNT', 'year': '2'}]
fields = ['name', 'branch', 'year']
filename = "branch_records.csv“
with open(filename, 'w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames = fields)
writer.writeheader()
writer.writerows(mydict)
print("Successfully File Write")
Prepared By : Radadiya Jitendra 41
JSON Module
 JSON (JavaScript Object Notation) is a popular data format
used for representing structured data.
 JSON (JavaScript Object Notation) was inspired by a subset of
the JavaScript programming language dealing with object literal
syntax.
 JSON used for Encode (Convert) Python objects to JSON type
and decode (Convert) JSON type to Python objects.
 Python has a built-in package called json, which can be use to
work with JSON data.
 To work with JSON, you can use Python's json module. You
need to import the module before you can use it.
import json
Prepared By : Radadiya Jitendra 42
JSON Module
Classes
1. JSONEncoder : An encoder class to convert Python object to JSON
format.
2. JSONDecoder : A decoder class to convert JSON format file into
Python object.
Functions
1. json.load(JSON_file) : Reading data from JSON file.
2. json.dump(obj, JSON_file) : Writing data in JSON file.
3. json.dumps(obj) : Convert Dictionary type ( Python object ) to
JSON file.
4. json.loads(JSON_file) : Convert JSON file to Dictionary type
(Python object ).
Prepared By : Radadiya Jitendra 43
JSON Module
Example : Python Reading JSON file
 Create JSON File name is person.json
{"name": “Raj", "languages": ["English", “Hindi"]}
import json
with open('person.json') as json_file:
data = json.load(json_file)
print(data)

Output :
{'name': ‘Raj', 'languages': ['English', ‘Hindi']}

Prepared By : Radadiya Jitendra 44


JSON Module
Example : Writing JSON to a file
import json
person_dict = {"name": “Raj", "languages": ["English", “Hindi"],
"age": 32 }
with open(‘json_person.json', 'w') as json_file:
json.dump(person_dict, json_file)
print("File Write Successfully")

Output :
File Write Successfully"

Prepared By : Radadiya Jitendra 45


JSON Module
Example : Convert Python (Dictionary type) to JSON

import json
person_dict = {'name': ‘Raj', 'age': 12, ‘stream’: ‘BCA’ }
person_json = json.dumps(person_dict)
print(person_json)

Output :
{"name": “Raj", "age": 12, “stream": “BCA”}

Prepared By : Radadiya Jitendra 46


JSON Module
Example : Convert JSON to Python (Dictionary type)
import json
person_json = '{"name": “Raj", "languages": ["English", “Hindi"]}‘
person_dict = json.loads(person_json)
print(person_dict)
print(person_dict['languages’])

Output :
{'name': ‘Raj', 'languages': ['English', ‘Hindi']}
['English', ‘Hindi']

Prepared By : Radadiya Jitendra 47


JSON Module
 Uses of JSON
1. It is used while writing JavaScript based applications that includes
browser extensions and websites.
2. JSON format is used for serializing and transmitting structured data
over network connection.
3. It is primarily used to transmit data between a server and web
applications.
4. Web services and APIs use JSON format to provide public data.
5. It can be used with modern programming languages.

Prepared By : Radadiya Jitendra 48


JSON Module
Example : Encoding using dump(), dumps() and JSON.Encoder class.
from io import StringIO
import json
fileObj = StringIO()
json.dump(["Hello", "Python"], fileObj)
print("Using json.dump(): "+str(fileObj.getvalue()))
class TypeEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, type):
return str(obj)

Prepared By : Radadiya Jitendra 49


JSON Module
Example : Encoding using dump(), dumps() and JSON.Encoder class.
print("Using json.dumps(): "+str(json.dumps(type(str),
cls=TypeEncoder)))
print("Using json.JSONEncoder().encode"+
str(TypeEncoder().encode(type(list))))
print("Using json.JSONEncoder().iterencode"+
str(list(TypeEncoder().iterencode(type(dict)))))

Prepared By : Radadiya Jitendra 50


JSON Module
Output :
Using json.dump(): ["Hello", "Python"]
Using json.dumps(): "<class 'type'>"
Using json.JSONEncoder().encode"<class 'type'>"
Using json.JSONEncoder().iterencode['"<class \'type\'>"']

Prepared By : Radadiya Jitendra 51


JSON Module
Example : Decoding using load(), loads() and JSON.Decoder class.
from io import StringIO
import json
fileObj = StringIO('["Hello to Python"]')
print("Using json.load(): "+str(json.load(fileObj)))
print("Using json.loads(): "+str(json.loads('{"Hello": 1, "to": 2,
"Python": 3}')))
print("Using json.JSONDecoder().decode(): " +
str(json.JSONDecoder().decode('{"Hello": 1, "to": 2, "Python": 3}')))
print("Using json.JSONDecoder().raw_decode(): " +
str(json.JSONDecoder().raw_decode('{"Hello": 1, "to": 2, "Python":
3}'))) Prepared By : Radadiya Jitendra 52
JSON Module
Output :
Using json.load(): ['Hello to Python']
Using json.loads(): {'Hello': 1, 'to': 2, 'Python': 3}
Using json.JSONDecoder().decode(): {'Hello': 1, 'to': 2, 'Python': 3}
Using json.JSONDecoder().raw_decode(): ({'Hello': 1, 'to': 2,
'Python': 3}, 34)

Prepared By : Radadiya Jitendra 53


XML Module
 The Extensible Markup Language (XML) is a markup language much
like HTML or SGML.
 XML is a portable, open source language that allows programmers to
develop applications that can be read by other applications,
regardless of operating system and/or developmental language.
 This is recommended by the World Wide Web and available as an
open standard.
XML Parser Architectures and APIs
 The Python standard library provides a minimal but useful set of
interfaces to work with XML.
 The two most basic and broadly used APIs to XML data are the SAX
and DOM interfaces.

Prepared By : Radadiya Jitendra 54


XML Module
Simple API for XML (SAX)

 This is useful when your documents are large or you have


memory limitations, it parses the file as it reads it from disk and the
entire file is never stored in memory.

 Here, you register callbacks for events of interest and then let
the parser proceed through the document.

Document Object Model (DOM) API

 This is a World Wide Web recommendation wherein the entire


file is read into memory and stored in a hierarchical (tree‐based)
form to represent all the features of an XML document.

Prepared By : Radadiya Jitendra 55


XML Module
 Document.documentElement : The one and only root element of
the document.

 Node.hasAttribute() : Returns true if the node has any attributes.

 Element.getAttribute(name) : Return the value of the attribute


named by name as a string. If no such attribute exists, an empty
string is returned, as if the attribute had no value.

 Document.getElementsByTagName(tagName) : Search for all


descendants (direct children, children’s children, etc.) with a
particular element type name.

 Node.childNodes : A list of nodes contained within this node. This is


a read-only attribute.
Prepared By : Radadiya Jitendra 56
XML Module
Create XML File movies.xml
<collection college=“PKM College">
<stream title="BCA">
<id>1</id>
<name>Raj</name>
<percentage>90</percentage>
<description>A Grade</description>
</stream>
<stream title="BCA">
<id>2</id>
<name>Ajay</name>
<percentage>70</percentage>
<description>B Grade</description>
Prepared By : Radadiya Jitendra 57
</collection>
XML Module
create Python File
from xml.dom.minidom import parse
import xml.dom.minidom
DOMTree = xml.dom.minidom.parse("movies.xml")
collection = DOMTree.documentElement
if collection.hasAttribute("college"):
print ("College Name : %s" % collection.getAttribute("college"))
s = collection.getElementsByTagName("stream")
for s1 in s:
print ("------------------")

Prepared By : Radadiya Jitendra 58


XML Module
if s1.hasAttribute("title"):
print ("Stream Name : %s" % s1.getAttribute("title"))
i = s1.getElementsByTagName('id')[0]
print ("Id is : %s" % i.childNodes[0].data)
name = s1.getElementsByTagName('name')[0]
print ("Name is : %s" % name.childNodes[0].data)
per = s1.getElementsByTagName('percentage')[0]
print ("Percentage is : %s" % per.childNodes[0].data)
description = s1.getElementsByTagName('description')[0]
print ("Description : %s" % description.childNodes[0].data)
Prepared By : Radadiya Jitendra 59
XML Module
Output :
College Name : PKM College
----------------------------
Stream Name : BCA
Id is : 1
Name is : Raj
Percentage is : 90
Description : A Grade
----------------------------
Stream Name : BCA
Id is : 2
Name is : Ajay
Percentage is : 70
Description : B Grade
Prepared By : Radadiya Jitendra 60
XML (Extensible Markup Language)
 SAX obviously cannot process information as fast as DOM can
when working with large files.
 On the other hand, using DOM exclusively can really kill your
resources, especially if used on a lot of small files.
 SAX is read-only, while DOM allows changes to the XML file.
 Since these two different APIs literally complement each other,
there is no reason why you cannot use them both for large
projects.

Prepared By : Radadiya Jitendra 61


XML (Extensible Markup Language)
Parsing XML with SAX APIs
 SAX is a standard interface for event-driven XML parsing.
 Parsing XML with SAX generally requires you to create your
own ContentHandler by subclassing xml.sax.ContentHandler.
 Your ContentHandler handles the particular tags and attributes
of your flavor(s) of XML.
 A ContentHandler object provides methods to handle various
parsing events.
 Its owning parser calls ContentHandler methods as it parses
the XML file.
 The methods startDocument and endDocument are called at
the start and the end of the XML file.
Prepared By : Radadiya Jitendra 62
XML (Extensible Markup Language)
Parsing XML with SAX APIs
 The method characters(text) is passed character data of the
XML file via the parameter text.
 The ContentHandler is called at the start and end of each
element.
 If the parser is not in namespace mode, the method
startElement(tag, attributes)and endElement(tag) are called,
otherwise, the corresponding methods startElemntNS and
endElementNS are called.
 Here, tag is the element tag, and attributes is an Attributes
object.

Prepared By : Radadiya Jitendra 63


XML (Extensible Markup Language)
The make_parser Method
 Following method creates a new parser object and returns it.
The parser object created will be of the first parser type the system
finds.
xml.sax.make_parser( [parser_list] )
 parser_list − The optional argument consisting of a list of parsers to
use which must all implement the make_parser method.

Prepared By : Radadiya Jitendra 64


XML (Extensible Markup Language)
The parse Method
 This method creates a SAX parser and uses it to parse a
document.
xml.sax.parse( xmlfile, contenthandler[, errorhandler])
 xmlfile − This is the name of the XML file to read from.
 contenthandler − This must be a ContentHandler object.
 errorhandler − If specified, errorhandler must be a SAX
ErrorHandler object.

Prepared By : Radadiya Jitendra 65


XML (Extensible Markup Language)
The parseString Method
 There is one more method to create a SAX parser and to parse the
specified XML string.
xml.sax.parseString(xmlstring, contenthandler[, errorhandler])
 xmlstring − This is the name of the XML string to read from.
 contenthandler − This must be a ContentHandler object.
 errorhandler − If specified, errorhandler must be a SAX
ErrorHandler object.

Prepared By : Radadiya Jitendra 66


XML (Extensible Markup Language)
Parsing XML with DOM APIs
 The Document Object Model ("DOM") is a cross-language API
from the World Wide Web Consortium (W3C) for accessing and
modifying XML documents.
 The DOM is extremely useful for random-access applications.
SAX only allows you a view of one bit of the document at a time. If
you are looking at one SAX element, you have no access to
another.
 Here is the easiest way to quickly load an XML document and
to create a minidom object using the xml.dom module.
 The minidom object provides a simple parser method that
quickly creates a DOM tree from the XML file.
Prepared By : Radadiya Jitendra 67

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy