Case Study: generating excel files - Plone sitegaspari/www/teaching/cp6b.pdf · 2019-11-21 · Case...

26
Mauro Gaspari - University of Bologna - [email protected] Case Study: generating excel files Chapter 14 Prof. Mauro Gaspari: [email protected]

Transcript of Case Study: generating excel files - Plone sitegaspari/www/teaching/cp6b.pdf · 2019-11-21 · Case...

Page 1: Case Study: generating excel files - Plone sitegaspari/www/teaching/cp6b.pdf · 2019-11-21 · Case Study: generating excel files Chapter 14 Prof. Mauro Gaspari: gaspari@cs.unibo.it.

Mauro Gaspari - University of Bologna - [email protected]

Case Study: generating excel files

Chapter 14

Prof. Mauro Gaspari: [email protected]

Page 2: Case Study: generating excel files - Plone sitegaspari/www/teaching/cp6b.pdf · 2019-11-21 · Case Study: generating excel files Chapter 14 Prof. Mauro Gaspari: gaspari@cs.unibo.it.

Mauro Gaspari - University of Bologna - [email protected]

Outline

● Two-dimensional tables● Persistent programs● Writing to files.● Exporting data to excel: the CSV format.● More on files

Page 3: Case Study: generating excel files - Plone sitegaspari/www/teaching/cp6b.pdf · 2019-11-21 · Case Study: generating excel files Chapter 14 Prof. Mauro Gaspari: gaspari@cs.unibo.it.

Mauro Gaspari - University of Bologna - [email protected]

Two-dimensional tables

● A two-dimensional table is a table where you read the value at the

intersection of a row and a column.

● Goal: print a multiplication table.

● Subgoal: print one line.

i = 1while i <= 10: print(2*i, '\t',end=’’) i = i + 1print()

Page 4: Case Study: generating excel files - Plone sitegaspari/www/teaching/cp6b.pdf · 2019-11-21 · Case Study: generating excel files Chapter 14 Prof. Mauro Gaspari: gaspari@cs.unibo.it.

Mauro Gaspari - University of Bologna - [email protected]

Encapsulating it!

● This function encapsulates the previous loop and generalizes it to

print multiples of n:

def printMultiples(n): i = 1 while i <= 10: print(n*i, '\t',end=’’) i = i + 1 print()

Page 5: Case Study: generating excel files - Plone sitegaspari/www/teaching/cp6b.pdf · 2019-11-21 · Case Study: generating excel files Chapter 14 Prof. Mauro Gaspari: gaspari@cs.unibo.it.

Mauro Gaspari - University of Bologna - [email protected]

Understanding this process

● Encapsulation and generalization

– To encapsulate, all we had to do was add the first line, which declares the name of the function and the parameter list.

– To generalize, all we had to do was replace the value 2 with the parameter n.

● This process is a common development plan:

– We develop code by writing lines of code outside any function, or typing them in to the interpreter.

– When we get the code working, we extract it and wrap it up in a function.

Page 6: Case Study: generating excel files - Plone sitegaspari/www/teaching/cp6b.pdf · 2019-11-21 · Case Study: generating excel files Chapter 14 Prof. Mauro Gaspari: gaspari@cs.unibo.it.

Mauro Gaspari - University of Bologna - [email protected]

Using this function

i = 1while i <= 10: printMultiples(i) i = i + 1

● Notice how similar this loop is to the one inside printMultiples. ● All we did was replace the print statement with a function call.● The output of this program is a multiplication table.

Page 7: Case Study: generating excel files - Plone sitegaspari/www/teaching/cp6b.pdf · 2019-11-21 · Case Study: generating excel files Chapter 14 Prof. Mauro Gaspari: gaspari@cs.unibo.it.

Mauro Gaspari - University of Bologna - [email protected]

More encapsulation

def printMultTable(): i = 1 while i <= 10: printMultiples(i) i = i + 1

● Now we are able to define a function that prints a multiplication

table.

● How we can use the same variable, i, in both printMultiples and

printMultTable. Doesn't it cause problems when one of the

functions changes the value of the variable?

Page 8: Case Study: generating excel files - Plone sitegaspari/www/teaching/cp6b.pdf · 2019-11-21 · Case Study: generating excel files Chapter 14 Prof. Mauro Gaspari: gaspari@cs.unibo.it.

Mauro Gaspari - University of Bologna - [email protected]

What is happening?

● The answer is no, because the i in printMultiples and the

i in printMultTable are not the same variable.

Page 9: Case Study: generating excel files - Plone sitegaspari/www/teaching/cp6b.pdf · 2019-11-21 · Case Study: generating excel files Chapter 14 Prof. Mauro Gaspari: gaspari@cs.unibo.it.

Mauro Gaspari - University of Bologna - [email protected]

More Generalization

● Goal: implement a program that would print a multiplication table

of any size, not just the ten-by-ten table.

● First: You could add a parameter to printMultTable.

def printMultTable(high): i = 1 while i <= high: printMultiples(i) i = i + 1

Page 10: Case Study: generating excel files - Plone sitegaspari/www/teaching/cp6b.pdf · 2019-11-21 · Case Study: generating excel files Chapter 14 Prof. Mauro Gaspari: gaspari@cs.unibo.it.

Mauro Gaspari - University of Bologna - [email protected]

NXN table

def printMultiples(n, high): i = 1 while i <= high: print(n*i, '\t',end=’’) i = i + 1 print()

def printMultTable(high): i = 1 while i <= high: printMultiples(i, high) i = i + 1

● Second: making printMultiples more general

Page 11: Case Study: generating excel files - Plone sitegaspari/www/teaching/cp6b.pdf · 2019-11-21 · Case Study: generating excel files Chapter 14 Prof. Mauro Gaspari: gaspari@cs.unibo.it.

Mauro Gaspari - University of Bologna - [email protected]

Persistent Programs

● Persistent programs: they keep at least some of their data in permanent storage (a hard drive, for example); and if they shut down and restart, they pick up where they left off. Examples:

– operating systems, which run pretty much whenever a computer is on;

– web servers, which run all the time, waiting for requests to come in on the network.

Page 12: Case Study: generating excel files - Plone sitegaspari/www/teaching/cp6b.pdf · 2019-11-21 · Case Study: generating excel files Chapter 14 Prof. Mauro Gaspari: gaspari@cs.unibo.it.

Mauro Gaspari - University of Bologna - [email protected]

Implementing persistent programs

● One of the simplest ways for programs to maintain their data is by reading and writing text files.

● An alternative is to store the state of the program in a database.

● We will also present a simple database and a module, pickle, that makes it easy to store program data.

Page 13: Case Study: generating excel files - Plone sitegaspari/www/teaching/cp6b.pdf · 2019-11-21 · Case Study: generating excel files Chapter 14 Prof. Mauro Gaspari: gaspari@cs.unibo.it.

Mauro Gaspari - University of Bologna - [email protected]

Files types● Text files: (in general having the .txt postfix)

contain ACSII or Unicode characters and can be created and read by most applications:

– Text editors (Notepad, SimpleText, etc.)– IDEs (Python IDLE) .py files are text files.– Word processors (MS Word, saving .txt)– Spreadsheets (Excel, saving .csv text format)

● Binary files: contain data coded in specific formats, for examples .jpg: pictures; .doc: documents; .mp3: music; xls: spreadsheet.

Page 14: Case Study: generating excel files - Plone sitegaspari/www/teaching/cp6b.pdf · 2019-11-21 · Case Study: generating excel files Chapter 14 Prof. Mauro Gaspari: gaspari@cs.unibo.it.

Mauro Gaspari - University of Bologna - [email protected]

Reading from text files

● The open statement: returns a file object for access with file

methods. Syntax:

fid = open(filename,mode)

● filename: should be a string containing the complete name of the

file, including the file extension and can include a partial or

complete path.

– In MS Windows, most file extensions are hidden in Windows Explorer

– Default path is the folder containing the main script (.py file).

● mode: can be 'r' read, 'w' write, 'a' append..

Page 15: Case Study: generating excel files - Plone sitegaspari/www/teaching/cp6b.pdf · 2019-11-21 · Case Study: generating excel files Chapter 14 Prof. Mauro Gaspari: gaspari@cs.unibo.it.

Mauro Gaspari - University of Bologna - [email protected]

Example● Create a file test.txt containing the following lines:

People are strange when you are a strangerFaces look ugly when you are aloneWomen seem wicked when you are unwantedStreets are uneven when you are down

>>> f = open('test.txt') >>> print(f)<open file 'test.txt', mode 'r' at fe820>>>> s1 = f.read()>>> f.seek(0)>>> s2 = f.readline()>>> f.seek(0)>>> s3 = f.readlines()

If no mode is specified 'r' is assumed.

Page 16: Case Study: generating excel files - Plone sitegaspari/www/teaching/cp6b.pdf · 2019-11-21 · Case Study: generating excel files Chapter 14 Prof. Mauro Gaspari: gaspari@cs.unibo.it.

Mauro Gaspari - University of Bologna - [email protected]

Open for writing

● To write a file, you have to open it with mode 'w' as a second parameter:

>>> f = open("test.dat","w")>>> print(f)<open file 'test.dat', mode 'w' at fe820>

● If the file already exists, opening it in write mode clears out the old data and starts fresh.

● If the file doesn’t exist, a new one is created.

Page 17: Case Study: generating excel files - Plone sitegaspari/www/teaching/cp6b.pdf · 2019-11-21 · Case Study: generating excel files Chapter 14 Prof. Mauro Gaspari: gaspari@cs.unibo.it.

Mauro Gaspari - University of Bologna - [email protected]

Writing a file

● The write method puts data into the file.

>>> f.write("Now is the time")>>> f.write("to close the file")>>> f.close() # close the file>>> f = open("test.dat","r")>>> f = open("test.cat","r")IOError: [Errno 2] No such file or directory: 'test.cat'>>> text = f.read()>>> print(text)Now is the timeto close the file

Page 18: Case Study: generating excel files - Plone sitegaspari/www/teaching/cp6b.pdf · 2019-11-21 · Case Study: generating excel files Chapter 14 Prof. Mauro Gaspari: gaspari@cs.unibo.it.

Mauro Gaspari - University of Bologna - [email protected]

Format operator>>> x = 52>>> f.write (str(x))>>> cars = 52>>> "%d" % cars'52'>>> cars = 52>>> "In July we sold %d cars." % cars'In July we sold 52 cars.'>>> "%d %d %d" % (1,2)TypeError: not enough arguments for format string>>> "%d" % 'dollars'>>> "%6d" % 62' 62'>>> "%12f" % 6.1' 6.100000'

Page 19: Case Study: generating excel files - Plone sitegaspari/www/teaching/cp6b.pdf · 2019-11-21 · Case Study: generating excel files Chapter 14 Prof. Mauro Gaspari: gaspari@cs.unibo.it.

Mauro Gaspari - University of Bologna - [email protected]

The CSV format

● Excel or calc (the similar open office application) can both import

data written in CSV format.

● The CSV ("Comma Separated Value") file format is used to

exchange data between disparate applications.

● Although new applications that wish to include an export format

will generally use XML, CSV files can still be considered a de

facto (legacy) industry standard.

Page 20: Case Study: generating excel files - Plone sitegaspari/www/teaching/cp6b.pdf · 2019-11-21 · Case Study: generating excel files Chapter 14 Prof. Mauro Gaspari: gaspari@cs.unibo.it.

Mauro Gaspari - University of Bologna - [email protected]

Exporting to excel

filename = input("Excel file name")realname = filename + ".csv"f = open(realname,"w")

def printMultiples(f, n, high): i = 1 while i <= high: f.write("%d ,"%(n*i)) #print n*i, '\t', i = i + 1 f.write('\n')

def printMultTable(f,high): i = 1 while i <= high: printMultiples(f,i, high) i = i + 1

printMultTable(f,20)f.close()

Page 21: Case Study: generating excel files - Plone sitegaspari/www/teaching/cp6b.pdf · 2019-11-21 · Case Study: generating excel files Chapter 14 Prof. Mauro Gaspari: gaspari@cs.unibo.it.

Mauro Gaspari - University of Bologna - [email protected]

More reading

>>> f = open("test.dat","r")>>> print(f.read(5))Now i>>> print(f.read(1000006))s the timeto close the file>>> print(f.read())

>>>

Page 22: Case Study: generating excel files - Plone sitegaspari/www/teaching/cp6b.pdf · 2019-11-21 · Case Study: generating excel files Chapter 14 Prof. Mauro Gaspari: gaspari@cs.unibo.it.

Mauro Gaspari - University of Bologna - [email protected]

Example

def copyFile(oldFile, newFile): f1 = open(oldFile, "r") f2 = open(newFile, "w") while 1: text = f1.read(50) if text == "": break f2.write(text) f1.close() f2.close() return

Page 23: Case Study: generating excel files - Plone sitegaspari/www/teaching/cp6b.pdf · 2019-11-21 · Case Study: generating excel files Chapter 14 Prof. Mauro Gaspari: gaspari@cs.unibo.it.

Mauro Gaspari - University of Bologna - [email protected]

Catching exceptions

● A lot of things can go wrong when you try to read and write files.

If you try to open a file that doesn’t exist, you get an ioError.

● The same happens if you don’t have permission to access a file.

● To avoid these errors, we need a construct to go ahead and try—

and deal with problems if they happen—which is exactly what the

try statement does.

Page 24: Case Study: generating excel files - Plone sitegaspari/www/teaching/cp6b.pdf · 2019-11-21 · Case Study: generating excel files Chapter 14 Prof. Mauro Gaspari: gaspari@cs.unibo.it.

Mauro Gaspari - University of Bologna - [email protected]

Example

filename = raw_input('Enter a file name: ')try: f = open (filename, "r")except: print('There is no file named', filename)

Page 25: Case Study: generating excel files - Plone sitegaspari/www/teaching/cp6b.pdf · 2019-11-21 · Case Study: generating excel files Chapter 14 Prof. Mauro Gaspari: gaspari@cs.unibo.it.

Mauro Gaspari - University of Bologna - [email protected]

The exists function

def exists(filename): try: f = open(filename) f.close() return 1 except: return 0

Page 26: Case Study: generating excel files - Plone sitegaspari/www/teaching/cp6b.pdf · 2019-11-21 · Case Study: generating excel files Chapter 14 Prof. Mauro Gaspari: gaspari@cs.unibo.it.

Mauro Gaspari - University of Bologna - [email protected]

Exercise 1

● Write a script that takes a string of digits as an input and returns the max the min and the average values.