0% found this document useful (0 votes)
58 views35 pages

4.file Operation, Pickle & Dictionary

This document discusses file operations, pickle, and dictionaries in Python. It begins with an overview of input and eval functions, then covers reading and writing to files, handling exceptions, and using the pickle module to serialize Python objects to files. It also explains how dictionaries store key-value pairs and common dictionary operations like accessing values, deleting items, and initializing from tuples. Keys must be immutable and duplicates are not allowed.

Uploaded by

Kezia Mally
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)
58 views35 pages

4.file Operation, Pickle & Dictionary

This document discusses file operations, pickle, and dictionaries in Python. It begins with an overview of input and eval functions, then covers reading and writing to files, handling exceptions, and using the pickle module to serialize Python objects to files. It also explains how dictionaries store key-value pairs and common dictionary operations like accessing values, deleting items, and initializing from tuples. Keys must be immutable and duplicates are not allowed.

Uploaded by

Kezia Mally
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/ 35

File Operations, Pickle &

Dictionary
Computer Science & Engineering Department
The Chinese University of Hong Kong

CSCI2040 INTRODUCTION TO PYTHON 1


Content
• input and eval
• File operations
• Pickle
• Dictionary

CSCI2040 INTRODUCTION TO PYTHON 2


Input
• We use input() to read input from user
input([prompt]) []: optional
• >>> num = input(“Enter a number: ”)
Enter a number: 36
>>> num
‘36’

• All input are in the form of strings


• We can use string methods to parse the content eg. split()
CSCI2040 INTRODUCTION TO PYTHON 3
Split()
>>> s1 ="學霸 學霸 學霸"
>>> s1
'學霸 學霸 學霸'
>>> s1.split()
['學霸', '學霸', '學霸']

Note : Python 2 require use of Unicode string to store non-ASCII strings


whereas Python 3 got only one string type str which default to Unicode

CSCI2040 INTRODUCTION TO PYTHON 4


Input
• We can then use int() or float() to convert into preferred
form

>>>int(‘36’)
36
>>>float(‘36’)
36.0

CSCI2040 INTRODUCTION TO PYTHON 5


What will be the result?
• int(‘12 int’)
• Error!
• int(’12.34’)
• Error
• int(‘12 ‘)
• 12
• int(‘1010’, 2)
• 10 int( x, base=10)
CSCI2040 INTRODUCTION TO PYTHON 6
Input
• We can use eval() to convert also

>>>eval(‘36’)
36
>>>eval(‘2+3*6’)
20

• Provided the string contains valid expression

CSCI2040 INTRODUCTION TO PYTHON 7


What will be the result?
• X=3
eval(‘2 * X’)
•6
• eval(‘ 2 and 3’)
•3
• eval(‘ 2 or 3’)
•2

CSCI2040 INTRODUCTION TO PYTHON 8


File
• Persistent storage even after program ends
• Represented in Python as type file (object)
• Typical file processing involves:
1. File open
2. Read/write operations
3. File close

CSCI2040 INTRODUCTION TO PYTHON 9


File
• In previous intro prog course, we learnt that file is usually
opened with access mode
file_handle = open( filename, mode)
• Typical mode:
1. “r” read
2. “w” write, create new file if not exists
3. “a” append to current file
4. “r+” read write access
5. “rb” if the file is binary , not text file
CSCI2040 INTRODUCTION TO PYTHON 10
File methods
f = open(“filename”) open a file, return file value
f = open(“filename”, encoding) open a file with encoding
f.read() return a single character value
f.read(n) return no more than n character values
f.readline() return the next line of input
f.readlines() return all the file content as a list
f.write(s) write string s to file
f.writelines(lst) write list lst to file
f.close() close file

CSCI2040 INTRODUCTION TO PYTHON 11


File
• readline return the next line of text, including a newline (return)
character at the end
• Returns empty string when file empty

• Using for statement can also have the same result

>>> f = open(‘message.txt‘)
>>> for line in f:
... print (line)

CSCI2040 INTRODUCTION TO PYTHON 12


Recovering from Exceptions
• File I/O operations can generate exceptions, an IOError
• Exception handling can prevent these errors

try:
f = open('input.txt')
except IOError as e:
print ('unable to open the file with error: "{}"'.format(e.args[-1]))
else:
print ('continue with processing')
f.close()
print ('continue')
>>> unable to open the file with error: "No such file or directory"
continue
CSCI2040 INTRODUCTION TO PYTHON 13
Handling files
• A better way is using with
• Ensure file is closed when the block inside with is exited

with open('hello.txt', 'w', encoding='utf-8') as outf:


# perform file operation
outf.write("Hello world")

print ('continue with processing‘)

CSCI2040 INTRODUCTION TO PYTHON 14


Operating System Command
• Useful OS command can be executed from python by including os module

>>> import os
>>> os.remove("gone.txt") # removing file

>>> os.curdir # get current directory


'.‘
>>> os.rename('oldfile.ext', 'newfile.ext')
>>>

CSCI2040 INTRODUCTION TO PYTHON 15


Standard I/O
• print writes characters to a file normally attached to display
window
• Input functions read from a file attached to keyboard
• These files can be accessed through sys module
• Input file : sys.stdin, output file: sys.stdout, error messages:
sys.stderr
• stderr normally goes also to stdout

CSCI2040 INTRODUCTION TO PYTHON 16


Standard I/O
• Can change these settings through sys
import sys
sys.stdout = open('output.txt', 'w')
sys.stderr = open('error.txt', 'w')
print ("see where this goes“)
print (5/4)
print (7.0/0)
sys.stdout.close()
sys.stderr.close() In output.txt
see where this goes
1
In error.txt
Traceback (most recent call last):
File "try1.py", line 6, in <module>
print (7.0/0)
ZeroDivisionError: float division by zero

CSCI2040 INTRODUCTION TO PYTHON 17


OS functions
• exit terminate a running Python program –
sys.exit(“message”)
• sys.argv is a list of command line options being passed

import sys
print ('argument of program are ', sys.argv)
>>>argument of program are
['D:/shor/COURSE/2040/lab/lab3/lab3.py']

If run directly in same folder


argument of program are [‘lab3.py']
CSCI2040 INTRODUCTION TO PYTHON 18
Pickle
• Useful in saving and restoring Python variables as an archive
• Also called serialization

import pickle # import to use


stackOne = Stack() # assume we have a stack data strcuture
stackTwo = Stack()
stackOne.push( 12 )
stackTwo.push( 'abc' )
stackOne.push( 23 )
stackOne.pop()
f = open('pickle1.pyp','w') # store the variables in a file
pickle.dump([stackOne, stackTwo], f)

CSCI2040 INTRODUCTION TO PYTHON 19


Pickle
• Later on in another program

>>> import pickle


>>> f = open('pickle1.pyp')
>>> [stackOne, stackTwo] = pickle.load(f)
>>> print stackOne.top()
12
>>> print stackTwo.top()
abc

CSCI2040 INTRODUCTION TO PYTHON 20


Dictionaries
• Indexed data structure - uses also square bracket notation
• Any immutable type can be used as index
• Braces create dictionary

>>> dct = { } # create new dictionary


>>> dct['name'] = “Donald Duck”
>>> dct['age'] = 63
>>> dct['eyes'] = “black”

• Index is called a key (LHS)


• Element stored that associated with key is called a Value (RHS)
CSCI2040 INTRODUCTION TO PYTHON 21
Dictionaries
• Also called maps, hashes or associative arrays
>>> print (dct['name'])
Donald Duck
>>> print dct.get('age')
63
>>> print (dct['weight'])
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
KeyError: 'weight‘

CSCI2040 INTRODUCTION TO PYTHON 22


Dictionaries
• Exception when no value with designated key
• Can be prevented by using built-in get method to check
>>> print (dct.get('weight', 0)) # 0 is default value
0 ntr kita return the default value
>>> dct['age'] = 21
>>> dct['age']
21

CSCI2040 INTRODUCTION TO PYTHON 23


Dictionaries
• Del used to delete an element from list

>>> del dct['age']


>>> print (dct['age'])
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
KeyError: 'age‘
>>>

• Can be initialized using colon ‘:’ separated tuple also


>>> info = {'name':'Batman', 'age':53, 'weight':200}
>>> print (info['name'])
Batman
CSCI2040 INTRODUCTION TO PYTHON 24
Dictionaries
• Dictionary values have no restriction, can be any object
• But two important properties

1. No duplicate keys allowed

>>> d ict = {'Name': 'Zara', 'Age': 7, 'Name': 'Manni'}


>>> print "dict['Name']: ", dict['Name']
dict['Name']: Manni
>>> dict
{'Name': 'Manni', 'Age': 7} all the key will be over written

CSCI2040 INTRODUCTION TO PYTHON 25


Dictionaries

2. Keys must be immutable i.e. lists not allowed

>>> dict = {['Name']: 'Zara', 'Age': 7}


Traceback (most recent call last):
dict = {['Name']: 'Zara', 'Age': 7};
TypeError: unhashable type: 'list'
>>>

CSCI2040 INTRODUCTION TO PYTHON 26


Dictionary operations
Operation Description
len(d) number of elements in d
d[k] item in d with key k
d[k]=v set item in d with key k to v
d.clear() remove all items from dictionary d
d.copy make a shallow copy of d
d.has_key(k) return 1 if d has key k, 0 otherwise
d.items() return a list of (key,value) pair
d.keys() return a list of keys in d
d.values return a list of values in d
d.get(k) same as d[k]
d.get(k,v) return d[k] if k is valid, otherwise return v
Return list in Python 2.7 or earlier,
but return view in 3.X or later
CSCI2040 INTRODUCTION TO PYTHON 27
Views Instead Of Lists

• Dict methods return “views” instead of lists in 3.X


• views like a window on the keys and values (or items) of a dictionary

>>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500}


>>> keys = dishes.keys()
>>> values = dishes.values()

>>> # view objects are dynamic and reflect dict changes


>>> del dishes['eggs']
>>> keys # No eggs anymore!
dict_keys(['sausage', 'bacon', 'spam'])
CSCI2040 INTRODUCTION TO PYTHON 28
Identifiers in Program
• Names of variables, functions, modules can collide with others – same
name used unintentionally (Python allows this)
• Managed using name spaces
• Encapsulation of names through levels of abstraction
• Three levels of encapsulation
• LEGB rule for simple variables
• Qualified names (explained in next chapter)
• modules (explained in next chapter)

29
LEGB
• L: local
• E: Enclosing function definitions
• G: Global
• B: built-in functions
• When Python is looking for meaning attached to a name, it
search the scope in the order: Local, Enclosing, Global, Built-in
>>> x = 42
>>> def afun():
...
...
x = 12
print (x)
Only creating local var
>>> afun()
12
>>> print (x)
42 30
Enclosing
• Occurs when one function is defined inside another
• Each function definition creates a new scope for variables at that level

>>> def a(x):


... def b(y):
... print (x + y)
... y = 11
... b(3)
... print (y)
...
>>> a(4)
7
11
31
Scopes
• Described as a series of nested boxes
• To find a match for a given variable, the boxes are examined from
inside out until the name is found
• Lambda create their own local scope
• Thus distinct from surrounding function scope

>>> def a(x):


... f = lambda x: x + 3
... print (f(3))
... print (x)
...
>>> a(4)
6
4
32
Built-in functions
• Functions that are initially part of any Python program e.g. open, zip,
etc
• Can be overridden in a different scope
• For example, a programmer can define his/her own open.
• However it will prevent access to the standard function i.e. file open

33
dir function
• dir can be used to access a list of names in current scope
• Get global scope in topmost level

>>> z = 12
>>> def dirTest():
... x = 34
... print (dir())
>>> print (dir())
['__builtins__', '__doc__', '__name__', '__package__',
'dirTest', 'pywin', 'z']
>>> dirTest()
['x']

34
dir function
• Can accept an argument
• Return scope of the object

>>> dir (dirtest)


['__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__',
'__dict__', '__doc__', '__format__', '__get__', '__getattribute__', '__globals__',
'__hash__', '__init__', '__module__', '__name__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',
'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals',
'func_name']
>>> import math
>>> dir(math)
['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2',
'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'exp', 'fabs', 'factorial', 'floor',
'fmod', 'frexp', 'fsum', 'hypot', 'isinf', 'isnan', 'ldexp', 'log', 'log10', 'log1p', 'modf', 'pi',
'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
35

You might also like