Author name: Prasanna

Python Data Persistence – Exceptions

Python Data Persistence – Exceptions

Even an experienced programmer’s code does contain errors. If errors pertain to violation of language syntax, more often than not, they are detected by the interpreter (compiler in case of C++/Java) and code doesn’t execute till they are corrected.

There are times though when the code doesn’t show syntax-related errors but errors creep up after running it. What is more, sometimes code might execute without errors and some other times, its execution abruptly terminates. Clearly, some situation that arises in a running code is not tolerable to the interpreter. Such a runtime situation causing the error is called an exception.
Take a simple example of displaying the result of two numbers input by the user. The following snippet appears error-free as far as syntax error is concerned.

Example

num1=int(input('enter a number..')) 
num2 = int(input(1 enter another number..')) 
result=num1/num2 
print ('result: ' , result)

When executed, above code gives satisfactory output on most occasions, but when num2 happens to be 0, it breaks, (figure 5.3)

enter a number . . 12
enter another number . . 3
result: 4.0
enter a number . . 12
enter another number . . 0
Traceback (most recent call last) :
File "E:\python37\tmp.py", line 3, in <module> 
result =num1/num2
ZeroDivisionError: division by zero

You can see that the program terminates as soon as it encounters the error without completing the rest of the statements. Such abnormal termination may prove to be harmful in some cases.
Imagine a situation involving a file object. If such runtime error occurs after the file is opened, the abrupt end of the program will not give a chance for the file object to close properly and it may result in corruption of data in the file. Hence exceptions need to be properly handled so that program ends safely.

If we look at the class structure of builtins in Python, there is an Exception class from which a number of built-in exceptions are defined. Depending upon the cause of exception in a running program, the object representing the corresponding exception class is created. In this section, we restrict ourselves to consider file operations-related exceptions.

Python’s exception handling mechanism is implemented by the use of two keywords – try and except. Both keywords are followed by a block of statements. The try: block contains a piece of code that is likely to encounter an exception. The except block follows the try: block containing statements meant to handle the exception. The above code snippet of the division of two numbers is rewritten to use the try-catch mechanism.

Example

try:
num1=int(input('enter a number..'))
num2=int(input('enter another number..'))
result=num1/num2
print (' result: ' , result)
except:
print ("error in division")
print ("end of program")

Now there are two possibilities. As said earlier, the exception is a runtime situation largely depending upon reasons outside the program. In the code involving the division of two numbers, there is no exception if the denominator is non-zero. In such a case, try: block is executed completely, except block is bypassed and the program proceeds to subsequent statements.

If however, the denominator happens to be zero, a statement involving division produces an exception. Python interpreter abandons rest of statements in try: block and sends the program flow to except: block where exception handling statements are given. After except: block rest of unconditional statements keep on executing, (figure 5.4)

enter a number..15 
enter another number..5 
result: 3.0
end of program 
enter a number..15 
enter another number..0 
error in division 
end of program

Here, except block without any expression acts as a generic exception handler. To catch objects of a specific type of exception, the corresponding Exception class is mentioned in front of except keyword. In this case, ZeroDivisionError is raised, so it is mentioned in except statement. Also, you can use the ‘as’ keyword to receive the exception object in an argument and fetch more information about the exception.

Example

try:
num1=int(input('enter a number..')) 
num2=int(input('enter another number..')) 
result =num1/num2 
print ('result: ', result) 
except ZeroDivisionError as e: 
print ("error message",e) 
print ("end of program")

File operations are always prone to raising exceptions. What if the file you are trying to open doesn’t exist at all? What if you opened a file in ‘r’ mode but trying to write data to it? These situations will raise runtime errors (exceptions) which must be handled using the try-except mechanism to avoid damage to data in files.
FileNotFoundError is a common exception encountered. It appears when an attempt to read a non-existing file. The following code handles the error.

Example

E:\python37>python tmp.py
enter a filename, .testfile.txt
H e l l o P y t h o n
end of program
E:\python37>python tmp.py
enter filename, .nofile.txt
error message [Errno 2] No such file or directory:
' nofile . txt '
end of program

Another exception occurs frequently when you try to write data in a file opened with ‘r’ mode. Type of exception is UnsupportedOperation defined in the io module.

Example

import io
try:
f=open ( ' testfile . txt1 , ' r ')
f.write('Hello')
print (data)
except io.UnsupportedOperation as e:
print ("error message",e)
print ("end of program")

Output

E:\python37 >python tmp.py 
error message not writable 
end of program

As we know, the write ( ) method of file objects needs a string argument. Hence the argument of any other type will result in typeError.

Example

try:
f=open (' testfile . txt' , ' w')
f.write(1234)
except TypeError as e:
print ("error message",e)
print ("end of program")

 

Output

E:\python37>python tmp.py
error message write() argument must be str, not int end of program

Conversely, for file in binary mode, the write () method needs bytes object as argument. If not, same TypeError is raised with different error message.

Example

try:
f =open ( ' testfile . txt' , ' wb' )
f.write('Hello1)
except TypeError as e:
print ("error message",e)
print ("end of program")

Output

E:\python37>python tmp.py
error message a bytes-like object is required, not 'str'
end of program

All file-related functions in the os module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type but are not accepted by the operating system.

Example

import os
try:
fd=os . open ( ' testfile . txt' , os . 0_RD0NLY | os . 0_CREAT) os.write(fd,'Hello'.encode())
except OSError as e:
print ("error message",e)
print ("end of program")

Output

E:\python37>python tmp.py
error message [Errno 9] Bad file descriptor 
end of program

In this chapter, we learned the basic file handling techniques. In the next chapter, we deal with advanced data serialization techniques and special-purpose file storage formats using Python’s built-in modules.

Python Data Persistence – Exceptions Read More »

Python Data Persistence – File/Directory Management Functions

Python Data Persistence – File/Directory Management Functions

We normally use operating system’s GUI utilities or DOS commands to manage directories, copy, and move files etc. The os module provides useful functions to perform these tasks programmatically. os .mkdir ( ) function creates a new directory at given path. The path argument may be absolute or relative to the current directory. Use chdir ( ) function to set current working directory at the desired path. The getcwd ( ) function returns the current working directory path.

Example

>>> os.mkdir('mydir')
>>> os.path.abspath('mydir')
'E:\\python37\\mydir'
>>> os.chdir('mydir')
>>> os.getcwd()
'E:\\python37\\mydir'

You can remove a directory only if the given path to rmdir ( ) function is not the current working directory path, and it is empty.

Example

>>> os.chdir( ' . . ' ) #parent directory becomes current working directory
>>> os.getcwd ( )
'E:\\python37'
>>> os.rmdir('mydir')

The rename ( ) and remove ( ) functions respectively change the name of a file and delete a file. Another utility function is listdir () which returns a list object comprising of file and subdirectory names in a given path.

Python Data Persistence – File/Directory Management Functions Read More »

Python Data Persistence – File Handling using os Module

Python Data Persistence – File Handling using os Module

Python’s built-in library has an os module that provides useful operating system-dependent functions. It also provides functions for performing low-level read/write operations on the file. Let us briefly get acquainted with them.

The open () function from the os module (obviously it needs to be referred to as os. open ( )) is similar to the built-in open ( ) function in the sense it also opens a file for reading/write operations. However, it doesn’t return a file or file-like object but a file descriptor, an integer corresponding to the file opened. File descriptor’s values 0, 1, and 2 are reserved for stdin, stout, and stder r streams. Other files will be given an incremental file descriptor. Also, the write ( ) and read( ) functions of the os module needs bytes to object as the argument. The os .open () function needs to be provided one or combinations of the following constants: (Table 5.2)

os.OWRONLYopen for writing only
os.ORDWRopen for reading and writing
os.OAPPENDappend on each write
os.OCREATcreate file if it does not exist
os.OTRUNCtruncate size to 0
os.OEXCLerror if create and file exists

As in the case of a file object, the os module defines a sleek () function to set file r/w position at the desired place from the beginning, current position, or end indicated by integers 0,1, and 2 respectively.

Example

>>> fd=os . open ("testfile. txt", os .0_RDWR | os .0_CREAT)
>>> text="Hello Python"
>>> encoded=text.encode(encoding='utf-16')
>>> os.write(fd, encoded)
>>> os.lseek(fd,0,0)
>>> encoded=os.read(fd)
>>> os.path.getsizeCtestfile.txt") #calculate file size
>>> encoded=os.read(fd, 26)
>>> text=encoded.decode('utf-16 ')
>>> text
'Hello Python'

 

Python Data Persistence – File Handling using os Module Read More »

Python Data Persistence – Simultaneous Read/Write

Python Data Persistence – Simultaneous Read/Write

Modes ‘w’ or ‘a’ allow a file to be written to, but not to be read from. Similarly, ‘r’ mode facilitates reading a file but prohibits writing data to it. To be able to perform both read/write operations on a file without closing it, add the ‘+’ sign to these mode characters. As a result ‘w+’, ‘r+’ or ‘a+’ mode will open the file for simultaneous read/write. Similarly, binary read/ write simultaneous operations are enabled on file if opened with ‘wb+’, ‘rb+’ or ‘ab+’ modes.

It is also possible to perform read or write operations at any byte position of the file. As you go on writing data in a file, the end of file (EOF) position keeps on moving away from its beginning. By default, new data is written at the current EOF position. When opened in ‘r’ mode, reading starts from the 0th byte i.e. from the beginning of the file.
The seek ( ) method of file object lets you set current reading or writing position to a desired byte position in the file. It locates the desired position by counting the offset distance from beginning (0), current position (1), or EOF (2). Following example illustrates this point:

Example

>>> file=open ("testfile . txt", "w+")
>>> file .write ("This is a rat race")
>>> file.seek(10,0) #seek 10th byte from beginning
>>> txt=file . read (3) #read next 3 bytes
>>> txt
' rat'
>>> file . seek (10,0) #seek 10th byte position
>>> file . write ('cat' ) #overwrite next 3 bytes
>>> file.seek(0)
>>> text=file . read () #read entire file
>>> text
'This is a cat race'
>>> file . close ( )

Of course, this may not work correctly if you try to insert new data as it may overwrite part of existing data. One solution could be to read the entire content in a memory variable, modify it, and rewrite it after truncating the existing file. Other built-in modules like file input and map allow modifying files in place. However, later in this book, we are going to discuss a more sophisticated tool for manipulating databases and update data on a random basis.

Python Data Persistence – Simultaneous Read/Write Read More »

Python Data Persistence – Write/Read Binary File

Python Data Persistence – Write/Read Binary File

When the mode parameter to open ( ) function is set to ‘w’ (or ‘a’) value, the file is prepared for writing data in text format. Hence write ( ) and writelines () methods send string data to the output file stream. The file is easily readable using any text editor. However, other computer files that represent pictures (.jpg), multimedia (.mp3, .mp4), executables (.exe, .com), databases (.db, .sqlite) will not be readable if opened using notepad like text editor because they contain data in binary format.

Python’s open( ) function lets you write binary data to a file. You need to add ‘b’ to the mode parameter. Set mode to ‘wb’ to open a file for writing binary data. Naturally, such a file needs to be read by specifying ‘rb’ as file opening mode.

In the case of ‘wb’ mode parameter, the write () method doesn’t accept a string as an argument. Instead, it requires a bytes object. So, even if you are writing a string to a binary file, it needs to be converted to bytes first. The encode ( ) method of string class converts a string to bytes using one of the defined encoding schemes, such as ‘utf-8’ or ‘utf-16’.

Example

>>> f=open('binary.dat' , 'wb')
>>> data='Hello Python'
>>> encoded=data.encode(encoding='utf-16')
>>> encoded
b'\xff\xfeH\x00e\x001\x001\x00o\x00 \x00P\x00y\x00t\ x00h\x00o\x00n\x00'
>>> f.write(encoded)
26
>>> f.close( )

To read back data from the binary files, the encoded strings will have to be decoded for them to be used normally, like printing it.

Example

> > > f=open('binary.dat', 'rb')
>>> encoded=f.read( )
>>> data=encoded.decode(encoding='utf-16')
>>> print (data)
Hello Python
>>> f.close( )

If you have to write numeric data to a binary file, number object is first converted to bytes and then provided as argument to write() method

Example

>>> f=open('binary.dat','wb')
>>> num=1234
>>> binl=num.to_bytes(16, 'big')
>>> f.write(bin1)
>>> f.close( )

To read integer data from binary file, it has to be extracted from bytes in the file.

Example

>>> f=open( ' binary.dat' , 'rb')
>>> data=f.read( )
>>> num=int.from_bytes(data,'big')
> > > num
1234
>>> f.close( )

In the to_bytes () and from_bytes () function, ‘big’ is the value of the byte order parameter. Writing float data to a binary file is a little complicated. You’ll need to use the built-in struct module and pack( ) function from it to convert float to binary. Explanation of struct module is beyond the scope of this book.

Example

>>> import struct
> > > numl=1234.56
>>> f=open('binary.dat','wb')
>>> binfloat = struct .pack ('f ' num1)
> > > f . write (binfloat)
4
> > > f.close ( )

To read back binary float data from file, use unpack () function.

Example

>>> f=open('binary.dat','rb')
>>> data=f.read( )
>>> numl=struct.unpack('f', data)
>>> print (num1)
(1234.56005859375,)
>>> f.close( )

 

Python Data Persistence – Write/Read Binary File Read More »

Python Data Persistence – Constraints

Python Data Persistence – Constraints

Constraints enforce restrictions on data that a column can contain. They help in maintaining the integrity and reliability of data in the table. Following clauses are used in the definition of one or more columns of a table to enforce constraints:

PRIMARY KEY: Only one column in a table can be defined to be a primary key. The value of this table will uniquely identify each row (a record) in the table. The primary key can be set to AUTOINCREMENT if its type is INTEGER. In that case, its value need not be manually filled.

NOT NULL: By default value for any column in a row can be left null. NOT NULL constraint ensures that while filling a new row in the table or updating an existing row, the contents of specified columns are not allowed to be null. In the above definition, to ensure that the ‘name’ column must have a certain value, NOT NULL constraint is applied to it.

FOREIGN KEY: This constraint is used to enforce the ‘exists’ relationship between two tables.
Let us create a Products table in ‘mydatabase’ that we created above. As shown in Figure 7.1, diagram, the ‘products’ table consists of ProductID, Name, and Price columns, with ProductlD as its primary key.

sqlite> CREATE TABLE Products (
. . . > ProductID INTEGER PRIMARY KEY
AUTOINCREMENT, 
...> Name TEXT (20),
...> Price INTEGER
. . . > ) ;

(Ensure that the SQL statement ends with a semi-colon. You may span one statement over multiple lines in the console)
We also create another ‘Customers’ table in the same database with CustlD and Name fields. The CustlD field should be defined as the primary key.

sqlite> CREATE TABLE Customers (
. . . > CustlD INTEGER PRIMARY KEY
AUTOINCREMENT, 
. . . > Name TEXT (20) ,
. . . > GSTIN TEXT (15)
. . . . > ) ;

Finally, we create another ‘Invoices’ table. As shown in the figure 7.1 diagram, this table has InvID as the primary key and two foreign key columns referring to ProductID in ‘Products’ table and CustlD in the ‘Customers’ table. The ‘Invoices’ table also contains the ‘price’ column.

sqlite> CREATE TABLE Invoices ( 
. . . > InvID INTEGER PRIMARY KEY
AUTOINCREMENT, 
. . . > CustlD INTEGER REFERENCES
Customers (CustlD), 
. . . > ProductID INTEGER REFERENCES
Products (ProductID), 
. . . > Quantity INTEGER (5)
. . . > ) ;

To confirm that our tables have been successfully created, use the .tables command:

sqlite> .tables
Customers Invoices Products

SQLite stores the schema of all databases in the SQL1TEJVIASTER table. We can fetch names of our databases and tables with the following command:

sqlite> SELECT * FROM s'qlite_master WHERE 
type='table';

To terminate the current session of SQLiteS activity use .quit command.

Python Data Persistence – Constraints Read More »

Python Data Persistence – json Module

Python Data Persistence – json Module

JavaScript Object Notation (JSON) is an easy-to-use lightweight data-interchange format. It is a language-independent text format, supported by many programming languages. This format is used for data exchange between the web server and clients. Python’s j son module, being a part of Python’s standard distribution, provides serialization functionality similar to the pickle module.

Syntactically speaking, json module offers identical functions for serialization and de-serialization of Python objects. Module-level functions dumps () and loads () convert Python data to its serialized string representation and vice versa. The dump () function uses a file object to persistently store serialized objects, whereas load () function reconstructs original data from the file.
Notably, dumps () function uses additional argument sort_keys. Its default value is False, but if set to True, JSON representation of Python’s dictionary object holds keys in a sorted order.

Example

>>> import json
>>> data=[{'product':'TV1, 1 brand':'Sam¬sung ', 'price' : 25000 }, {1 product' : 'Computer', 'br
and':'Dell','price':40000},{'product':'Mo¬bile ', 'brand' :'Redmi', 'price' : 15000} ]
>>> JString=json.dumps(data, sort_keys=True) .
>>> JString
'[{"brand": "Samsung", "price": 25000, "product": "TV"}, {"brand": "Dell", "price": 40000, "product": "Computer"}, {"brand": "Redmi", "price": 15000, "product": "Mobile"}]'

The loads () function retrieves data in the original format.

Example

>>> string=json.loads(JString)
>>> string
[{'brand': 'Samsung', 'price': 25000, 'product':
'TV'}, {'brand': 'Dell', 'price': 40000, 'product': 'Computer'}, {'brand': 'Redmi', 'price': 15000,
'product': 'Mobile'}]

To store/retrieve JSONed data to/from a disk file, use dump () and load () functions respectively.

Example

>>> file=open ( ' j son. txt' , ' w' )
>>> j son. dump (data, file)
>>> file=open ( ' j son. txt' , ' r ' )
>>> data=j son. load (file)

Python’s built-in types are easily serialized in JSON format. Conversion is done, as per the corresponding table below:ftaZ>/e 6.1)

PythonJSON
DictObject
List , tupleArray
StrString
Int , float , int-& float-derived EnumsNumber
TrueTrue
FalseFalse
NoneNull

However, converting objects of a custom class is a little tricky. The json module defines JSONEndoder and JSONDecoder classes. We need to subclass them to perform encoding and decoding of objects of user-defined classes.

The following script defines a User class and an encoder class inherited from the JSONEncoder class. This subclass overrides the abstract default () method to return a serializable version of the User class which can further be encoded.

Example

import json
class User:
      def__init__(self,name, email, pw):
             self.name=name
             self.email=email
             self.pw=pw
     def__str__(self):
return ('Name: {} email: {} password: {}'. \ format(self.name, self.email, self.pw)
class UserEncoder(json.JSONEncoder):
   def default(self, z) :
       if isinstance(z, User):
           return (z.name, z.email, z.pw)
     else:
           super().default(self, z)
usdr1=User('Raj an','[email protected]','**')
encoder=UserEncoder( )
obj =encoder.encode(user1)
file=open (' j sonOO. txt' , ' w ' )
json.dump (obj , file)
file. close ()

To obtain the original object, we need to use a subclass of JSONDecoder. The subclass should have a method that is assigned a value of the object_hook parameter. This method will be internally called when an object is sought to be decoded.

Example

import j son
class UserDecoder(json.JSONDecoder):
         def__init__(self):
              j son.JSONDecoder.__init__
(self,object_hook=self.hook)
         def hook(self,obj):
               return diet(obj)
decoder=UserDecoder()
file=open (' j sonclass . txt' , ' r' )
retobj =j son. load (file)
print (decoder.decode(retobj))

The j son. tool module also has a command-line interface that validates data in string or file and delivers nice formatted output of JSON objects.
Assuming that the current working directory has ‘file.txt’ that contains text in JSON format, as below:

Example

{"name": "Rajan", "email": "[email protected]", "pw": "**"}

The following command produces pretty print output of the above string.

E: \python37>python -m j son. tool file.txt
{
"name": "Rajan",
"email": "rxa.com",

It accepts a -sort-keys command line option to display keys in ascending order.

E:\python3 7>python -m json.tool file.txt --sort-keys
{
"email": " rwa.com",
"name": "Raj an",
"pw" : " * * "
}

Python Data Persistence – json Module Read More »

Python Data Persistence – xml Package

Python Data Persistence – xml Package

XML is another well-known data interchange format, used by a large number of applications. One of the main features of extensible Markup Language (XML) is that its format is both human-readable and human-readable. XML is widely used by applications of web services, office tools, and Service-Oriented Architectures (SOA).

Standard Python library’s xml package consists of modules for XML processing, as per different models. In this section, we discuss the ElementTree module that provides a simple and lightweight API for XML processing.
The XML document is arranged in a tree-like hierarchical format. The document tree comprises of elements. Each element is a single node in the tree and has an attribute enclosed in <> and </> tags. Each element may have one or more sub-elements following the same structure.

A typical XML document appears, as follows:

Example

<?xml version="l.0" encoding="iso-8859-l"?> 
<pricelist>
     <product>
              <name >TV</name >
              <brand>Samsung</brand>
              <price>25000</price>
   </product>
   <product>
             <name >Computer</name >
             <brand>Dell</brand>
             <price>40000</price>
   </product>
   <product>
            <name >Mobile </name >
            <brand>Redmi</brand>
            <price>15000</price>
   </product>
</pricelist>

The elementary module’s class structure also has Element and SubElement objects. Each Element has a tag and attrib which is a diet object. For the root element, attrib is an empty dictionary.

Example

>>> import xml.etree.ElementTree as xmlobj
>>> root=xmlobj.Element('PriceList')
>>> root.tag
'PriceList'
>>> root.attrib
{ }

Now, we can add one or more nodes, i.e., elements under root. Each Element object may have SubElements, each having an attribute and text property.

Let us set up the ‘product’ element and ‘name’, ‘brand’, and ‘price’ as its sub-elements.

Example

>>> product=xmlobj.Element('Product')
>>> nm=xmlobj.SubElement(product, 'name')
>>> nm.text='name'
>>> brand=xmlobj.SubElement(product, 'brand')
>>> nm.text='TV'
>>> brand.text='Samsung'
>>> price=xmlobj.SubElement(product, 'price')
>>> price.text='25000'

The root node has an append ( ) method to add this node to it.

Example

>>> root.append(product)

Construct a tree from this root object and write its contents to the XML file.

Example

>> > tree=xmlobj.ElementTree (root)
> > > file=open ( ' pricelist. xml ,'wb')
>>> tree. write (file)
>>> file . close ( )

The ‘pricelist.xmP should be visible in the current working directory. The following script writes a list of dictionary objects to the XML file:

Example

import xml.etree.ElementTree as xmlobj
root=xmlobj.Element(1PriceList')
pricelist=[{'name' :'TV', 'brand1 :'Sam¬sung ' , 'price' : '250001 } ,
{'name' :'Computer', 'brand' :1 Dell1, 'pri ce' : '400001 } ,
{'name' :1 Mobile', 'brand':'Red- mi 1 price':'150001}]
i = 0
for row in pricelist:
i = i + 1
print (i)
element=xmlobj.Element('Product'+str(i))
for k,v in row.items():
sub=xmlobj.SubElement(element, k)
sub.text=v
root.append(element)
tree=xmlobj.ElementTree(root)
file=open ( ' pricelist. xml' , 1 wb ' )
tree . write (file)
file . close ()

To parse the XML file, construct document tree giving its name as file parameter in ElementTree constructor.

Example

import xml.etree.ElementTree as xmlobj
tree = xmlobj .ElementTree (file='pricelist .xml' )

The getroot() method of tree object fetches root element and getchildren() returns a list of elements below it.

Example

root = tree.getroot( ) 
children = root.getchildren()

We can now construct a dictionary object corresponding to each subelement by iterating over the sub-element collection of each child node.

Example

for child in children:
product={ }
pairs = child.getchildren()
for pair in pairs:
product[pair.tag]=pair.text

Each dictionary is then appended to a list returning original list of dictionary objects. Complete code parsing XML file into a list of dictionaries is as follows:

Example

import xml.etree.ElementTree as xmlobj
tree = xmlobj .ElementTree (file='pricelist .xml' )
root = tree.getroot()
products= [ ]
children = root.getchildren() for child in children:
product={ }
pairs = child.getchildren()
for pair in pairs:
product[pair.tag]=pair.text products.append(product)
print (products)

Save the above script from ‘xmlreader.py’ and run it from the command line:

E:\python37 >python xmlreader.py [{'name': 'TV', 'brand': 'Samsung', 'price':
'250001 }, { 'name' : 'Computer' , 'brand' : 'Dell' ,
'price': '40000'}, {'name': 'Mobile', 'brand':
'Redmi', 'price': '15000'}]

Of other modules in xml package, xml. dom implements document object model of XML format and xm‘1. sax defines functionality to implement SAX model.

Python Data Persistence – xml Package Read More »

Python Data Persistence – plistlib Module

Python Data Persistence – plistlib Module

Lastly, we have a look at plist module that used to read and write ‘property list’ files (they usually have .plist’ extension). This type of file is mainly used by MAC OS X. These files are essentially XML documents, typically used to store and retrieves properties of an object.
The functionality of plastic module is more or less similar to other serialization libraries. It defines dumps () and loads () functions for the string representation of Python objects. The load () and dump () functions read and write plist disk files.
The following script stores a diet object to a plist file.

Example

import plistlib
proplist = {
"name" : "Ramesh",
"class":"XII",
"div":"B",
"marks" : {"phy":50, "che" 60, "maths":80}
}
fileName=open ('marks .plist' ' wb' )
plistlib . dump (proplist, fileName)
fileName . close ()

The load() function retrieves an identical dictionary object from the file.

Example

with open('marks.plist', 'rb') as fp:
p1 = plistlib.load(fp) 
print(p1)

Another important data persistence library in Python is the sqlite3 module. It deals with read/write operations on the SQLite relational database. Before we explore its functionality, let us get acquainted with RDBMS concepts and the basics of SQL, which is the next chapter.

Python Data Persistence – plistlib Module Read More »

Python Data Persistence – csv module

Python Data Persistence – csv module

The Comma Separated Values (CSV) format is very widely used to import and export data in spreadsheets and RDBMS tables. The csv module, another built-in module in Python’s standard library, presents the functionality to easily convert Python’s sequence object in CSV format and write to a disk file. Conversely, data from CSV files is possible to be brought in Python namespace. The reader and writer classes are defined in this module that performs read/write operations on CSV files. In addition, this module also has DictReader and DxctWriter classes to work with Python’s dictionary objects.

The object of the writer class is obtained by the writer () constructor which needs a file object having ‘w’ mode enabled. An optional ‘dialect’ parameter is given to specify the implementation type of CSV protocol, which is by default ‘excel’ – the format preferred by MS Excel spreadsheet software. We are now in a position to write one or more rows to the file represented by the writer object.

Example

>>> import csv
>>> data=[('TV', 1 Samsung',25000) , ( 'Comput-
er1 'Dell40000),('Mobile', 'Redmi ',15000)]
> > > file=open (' pricelist .csv' , 'w' , newline='')
>>> obj =csv. writer (file)
>>> #write single row
>>> obj.writerow(data[0])
>>> #write multiple rows
> > > obj.writerows(data [1:])
> > > file . close ( )

Note that, open( ) function needs the newline=’ ‘ parameter to correctly interpret newlines inside quoted fields. The ‘pricelist.csv’ should be created in the current working directory. Its contents can be verified by opening in any text editor, as per your choice.
The reader object, on the other, hand returns an iterator object of rows in the CSV file. A simple for loop or next() function of an iterator can be used to traverse all rows.

Example

>>> file=open ('pricelist .csv' , ' r ' newline=' ' )
>>> obj =csv. reader (file)
>>> for row in obj:
print (row)
['TV', 'Samsung', 25000']
['Computer', 'Dell , '40000']
['Mobile', 'Redmi' '15000']
>>>

The csv module offers powerful DictWriter and DictReader classes that can deal with dictionary objects. DictWriter maps the sequence of dictionary objects to rows in the CSV file. As always, the DictWriter constructor needs a writable file object. It also needs a fieldnames parameter whose value has to be a list of fields. These fields will be written as a first row in the resultant CSV file. Let us convert a list of tuples, in the above example, to a list of diet objects and send it to csv format using the DictWriter object.

Example

>>> data=[{'product':'TV', 'brand1:1 Samsung ','price':25000}, {'product':'Computer','br and':'Dell','price':40000},{'product':'Mo¬bile ', 'brand' :'Redmi', 'price' :15000 } ]
>>> file=open (' pricelist. csv' , ' w' , newline= ' ')
>>> fields=data [0] .keys()
>>> obj =csv.DictWriter (file,fields)

The DictWriter’s write header () method uses fieldnames parameter to write header row in CSV file. Each row following the header contains the keys of each dictionary item.

Example

>>> obj.writeheader() 
>>> obj.writerows(data)
>>> 'file . close ()

The resulting ‘pricelist.csv’ will show data, as follows:

Example

product,brand,price 
TV,Samsung, 2 5000 
Computer,Dell ,4 0000 
Mobile,Redmi,15000

Reading rows in dictionary formation is easy as well. The DictReader object is obtained from the source CSV file. The object stores strings in the first row in field name attributes. A simple for loop can fetch subsequent rows. However, each row returns an OrderedDict object. Use diet () function to obtain a normal dictionary object out of each row.

Example

>>> file=open ('pricelist. csvr ,newline='')
>>> obj=csv.DictReader (file)
>>> obj .fieldnames
['product1, 'brand', 'price']
>>> for row in obj:
print (diet(row))
{'product': 'TV', 'brand': 'Samsung', price': '25000'}
{'product': 'Computer', 'brand' 'Dell ' , ' price' : '40000'}
{'product': 'Mobile', 'brand': Redmi' 'price': ' 15000'}

 

Python Data Persistence – csv module Read More »