Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class 9 Reading and writing to files chr, ord and Unicode

Similar presentations


Presentation on theme: "Class 9 Reading and writing to files chr, ord and Unicode"— Presentation transcript:

1 Class 9 Reading and writing to files chr, ord and Unicode
functions without parameters or returns values

2 File Processing Working with text files in Python
Associate a disk file with a file object using the open function <filevar> = open(<name>, <mode>) name is a string with the actual file name on the disk. The mode is either ‘r’ or ‘w’ depending on whether we are reading or writing the file. infile = open("numbers.dat", "r") outfile = open("results.txt", "w") Python Programming, 3/e

3 File processing input file – problem if not found
output file – overwrites existing file

4 File Methods <file>.read() – returns the entire remaining contents of the file as a single (possibly large, multi-line) string <file>.readline() – returns the next line of the file. This is all text up to and including the next newline character <file>.readlines() – returns a list of the remaining lines in the file. Each list item is a single line including the newline characters. Python Programming, 3/e

5 File Processing Opening a file for writing prepares the file to receive data Don't forget to close! If you open an existing file for writing, you wipe out the file’s contents. If the named file does not exist, a new one is created. outfile = open("mydata.out", "w") print(<expressions>, file=outfile) Python Programming, 3/e

6 Some solutions to lab 8 and how they work (and their limitations.)

7 Lab8AWLv1.py #Lab8AWLv1 # get the input file name and open the file filename=input("Enter input file name: ") infile=open(filename,"r") # get the output file name and open the file filename=input("Enter the output file name: ") outfile=open(filename,"w") # read file using readlines, and print to screen saying=infile.readlines() print(saying) # for each line in the saying, # compute the average word length for line in saying: sum=0.0 count=0 for word in line.split(): print(word, end="-") sum=sum+len(word) count=count+1 print() #skip to next line #print the result print(line, file=outfile) print(sum, count, sum/count, file=outfile) infile.close() outfile.close()

8 Notes on solution readlines – what does it return? What is saying? What is line.split()? What is word? accumulating sum (of the number of letters) counting re-initialize for each line

9 Lab8AWLv1.py Can it handle any file of lines of text?
Does it count the punctuation as letters? Does it count newlines as letters? Does the input file need a newline after the last line of text? Why the funny spacing?

10 Seasons.txt Seasons.txt Thirty days hath September April, June, and November All the rest have thirty-one Except February, which has twenty-eight.

11 SeasonsOut.txt Thirty days hath September April, June, and November All the rest have thirty-one Except February, which has twenty-eight

12 shell output ['Thirty days hath September\n', 'April, June, and November\n', 'All the rest have thirty-one\n', 'Except February, which has twenty-eight.'] Thirty-days-hath-September- April,-June,-and-November- All-the-rest-have-thirty-one- Except-February,-which-has-twenty-eight.-

13 fastAnimals.txt cat can run horses really gallop

14 fastAnimalsOut.txt cat can run horses really gallop

15 fastAnimals shell output
['cat can run\n', 'horses really gallop'] cat-can-run- horses-really-gallop-

16 Lab8AWLv2.py #Lab8AWLv2 # get the input file name and open the file filename=input("Enter input file name: ") infile=open(filename,"r") # get the output file name and open the file filename=input("Enter the output file name: ") outfile=open(filename,"w") # read file using readline, and print to screen # for each line in the saying, # read the line from the file and # compute the average word length for k in range(4): line=infile.readline() print(line) sum=0.0 count=0 for word in line.split(): print(word, end="-") sum=sum+len(word) count=count+1 print() #skip to next line #print the result print(line, file=outfile) print(sum, count, sum/count, file=outfile) infile.close() outfile.close()

17 Lab8AWLv2.py Can it handle any file of lines of text?
Does it count the punctuation as letters? Does it count newlines as letters? Does the input file need a newline after the last line of text? Why the funny spacing?

18 Lab8AWLv2.py Can it handle any file of lines of text? Must have 4
Does it count the punctuation as letters? Yes Does it count newlines as letters? No Does the input file need a newline after the last line of text? No Why the funny spacing? stores the newline

19 Lab8AWLv3.py #Lab8AWLv3 #print to the screen not to an output file # get the input file name and open the file filename=input("Enter input file name: ") infile=open(filename,"r") # read file using readlines, and print to screen saying=infile.readlines() print(saying) # for each line in the saying, # compute the average word length for line in saying: print(line) lineSplit=line.split() wordsJoin="".join(lineSplit) #the words #without spaces letterCount=len(wordsJoin) wordsCount=len(lineSplit) #length of a list avg=letterCount/wordsCount #print the result print(letterCount, wordsCount, avg) infile.close()

20 Coding and Encryption See discussion in the book

21 String Representation
In the early days of computers, each manufacturer used their own encoding of numbers for characters. ASCII system (American Standard Code for Information Interchange) uses 127 bit codes Python supports Unicode (100,000+ characters) Python Programming, 3/e

22 String Representation
The ord function returns the numeric (ordinal) code of a single character. The chr function converts a numeric code to the corresponding character. >>> ord("A") 65 >>> ord("a") 97 >>> chr(97) 'a' >>> chr(65) 'A' Python Programming, 3/e

23 Building a string accumulate a string First initialize it: message=""
as we go through loop adding characters (or other strings): message= message + <string>

24 Lists Have Methods, Too The append method can be used to add an item at the end of a list. squares = [] for x in range(1,101): squares.append(x*x) We start with an empty list ([]) and each number from 1 to 100 is squared and appended to it ([1, 4, 9, …, 10000]). Python Programming, 3/e

25 Unicode str to convert a number into a string (so can concat, for exam) Not the same as chr!


Download ppt "Class 9 Reading and writing to files chr, ord and Unicode"

Similar presentations


Ads by Google