Fundamentals of Python: First Programs - Strings and Text Files PDF
Document Details
Uploaded by FaithfulSymbol
Tags
Summary
This document is a chapter from a textbook on Python programming, focusing on strings and text files. It covers topics like accessing characters, substrings, file manipulation and Python methods. The chapter explains how to work with text files and manipulate strings in Python.
Full Transcript
Fundamentals of Python: First Programs Second Edition Chapter 4 Strings and Text Files © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except...
Fundamentals of Python: First Programs Second Edition Chapter 4 Strings and Text Files © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 1 Objectives (1 of 2) 4.1 Access individual characters in a string 4.2 Retrieve a substring from a string 4.3 Search for a substring in a string © 2018 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. Objectives (2 of 2) 4.5 Use string methods to manipulate strings 4.6 Open a text file for output and write strings or numbers to the file 4.7 Open a text file for input and read strings or numbers from the file 4.8 Use library functions to access and navigate a file system © 2018 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. Accessing Characters and Substrings in Strings In this section, we examine the internal structure of a string more closely You will learn how to extract portions of a string called substrings © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 4 The Structure of Strings An integer can’t be factored into more primitive parts A string is a data structure Data structure: Consists of smaller pieces of data String’s length: Number of characters it contains (0+) The len function returns the string’s length, which is the number of characters it contains >>> len(“Hi there!”) 9 >>> len(“”) 0 © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 5 The Subscript Operator (1 of 2) The form of the subscript operator is: [] Example: >>> name = “Alan Turing” >>> name # Examine the first character ‘A’ >>> name # Examine the fourth character ‘n’ >>> name[len(name)] # Oops! An index error! Traceback (most recent call last): File “”, line 1, in IndexError: string index out of range >>> name[len(name) − 1] # Examine the last character ‘g’ >>> name[−l] # Shorthand for the last character ‘g’ >>> name[−2] # Shorthand for next to last character ‘n’ © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 6 The Subscript Operator (2 of 2) Subscript operator is useful when you want to use the positions as well as the characters in a string Use a count-controlled loop >>> data = “Hi there!” >>> for index in range(len(data)): print(index, data[index]) 0H 1i 2 3t 4h 5e 6r 7e 8! © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 7 Slicing for Substrings Python’s subscript operator can be used to obtain a substring through a process called slicing Place a colon (:) in the subscript; an integer value can appear on either side of the colon >>> name = “myfile.txt” # The entire string >>> name[0:] ‘myfile.txt’ >>> name[0:1] # The first character ‘m’ >>> name[0:2] # The first two characters ‘my’ >>> name[:len(name)] # The entire string ‘myfile.txt’ >>> name[−3:] # The last three characters ‘txt’ >>> name[2:6] # Drill to extract 'file' ‘file’ © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 8 Testing for a Substring with the in Operator When used with strings, the left operand of in is a target substring and the right operand is the string to be searched Returns True if target string is somewhere in search string, or False otherwise This code segment traverses a list of filenames and prints just the filenames that have a.txt extension: >>> fileList = [“myfile.txt”, “myprogram.exe”, “yourfile.txt”] >>> for fileName in fileList: if “.txt” in fileName: print(fileName) myfile.txt yourfile.txt © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 9 String Methods (1 of 3) Python includes a set of string operations called methods that make tasks like counting the words in a single sentence easy >>> sentence = input(“Enter a sentence: ”) Enter a sentence: This sentence has no long words. >>> listOfWords = sentence.split() >>> print(“There are”, len(listOfWords), “words. ”) There are 6 words. >>> sum = 0 >>> for word in listOfWords: sum += len(word) >>> print(“The average word length is”, sum / len(listOfWords)) The average word length is 4.5 © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 10 String Methods (2 of 3) A method behaves like a function, but has a slightly different syntax A method is always called with a given data value called an object.(,..., ) Methods can expect arguments and return values A method knows about the internal state of the object with which it is called In Python, all data values are objects View a complete list and documentation of string methods by entering dir(str) at a shell prompt; you enter help(str.) to receive documentation on an individual method © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 11 String Methods (3 of 3) Example: extracting a filename’s extension >>> "myfile.txt".split('.') ['myfile', 'txt'] >>> "myfile.py".split('.') ['myfile', 'py'] >>> "myfile.html".split('.') ['myfile', 'html'] The subscript [−1] extracts the last element Can be used to write a general expression for obtaining any filename’s extension, as follows: filename.split('.')[−1] © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 12 Text Files A text file is software object that stores data on permanent medium such as disk or CD When compared to keyboard input from human user, the main advantages of taking input data from a file are: The data set can be much larger The data can be input much more quickly and with less chance of error The data can be used repeatedly with the same program or with different programs © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 13 Text Files and Their Format Using a text editor such as Notepad or TextEdit, you can create, view, and save data in a text file A text file containing six floating-point numbers might look like: 34.6 22.33 66.75 77.12 21.44 All data output to or input from a text file must be 99.01 strings Number must be converted to string before output © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 14 Writing Text to a File Data can be output to a text file using a file object To open a file for output: >>> f = open("my file.txt", 'w') If file does not exist, it is created If it already exists, Python opens it; when data are written to the file and the file is closed, any data previously existing in the file are erased This statement writes two line of text to the >>>file: f.write("First line.\nSecond line.\n") When >>> all outputs are finished, close the file: f.close() © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 15 Writing Numbers to a File The file method write expects a string as an argument Other types of data must first be converted to strings before being written to output file (e.g., using str) Code segment that illustrates the output of integers to a text file import random f = open(“integers.txt”, ‘w’) for count in range(500): number = random.randint(1, 500) f.write(str(number) + ‘\n’) f.close() © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 16 Reading Text from a File (1 of 2) You open a file for input in a manner similar to opening a file for output >>> f = open(“my file.txt”, ‘r’) If the path name is not accessible from the current working directory, Python raises an error There are several ways to read data from a file Example: the read method >>> text = f.read() >>> text ‘First line.\nSecond line.\n’ >>> print(text) First line. Second line. © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 17 Reading Text from a File (2 of 2) After input is finished, read returns an empty string >>> f = open("my file.txt", 'r') >>> for line in f: print(line) First line. Second line. Next code segment inputs lines of text with readline: >>> f = open(“my file.txt”, ‘r’) >>> while True: line = f.readline() if line == “ ”: break print(line) First line. Second line. © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 18 Reading Numbers from a File (1 of 3) In Python, string representations of integers and floating-point numbers can be converted to the numbers by using the functions int and float ‘r’) f = open(“integers.txt”, theSum = 0 for line in f: line = line.strip() number = int(line) theSum += number print(“The sum is”, theSum) © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 19 Reading Numbers from a File (2 of 3) The next code segment modifies the previous one to handle integers separated by spaces and/or newlines f = open(“integers.txt”, ‘r’) theSum = 0 for line in f: wordlist = line.split() for word in wordlist: number = int(word) theSum += number print(“The sum is”, theSum) © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 20 Reading Numbers from a File (3 of 3) Method What it Does open(filename, Opens a file at the given filename and mode) returns a file object. The mode can be ‘r’, ‘w’, ‘rw’, or ‘a’. The last two values mean read/write and append. f.close() Closes an output file. Not needed for input files. f.write(aString) Outputs aString to a file. f.read() Inputs the contents of a file and returns them as a single string. Returns “” if the end of file is reached. f.readline() Inputs a line of text and returns it as a string, including the newline. Returns “” if the end of file is reached. © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 21 Accessing and Manipulating Files and Directories on Disk (1 of 5) The complete set of directories and files forms a tree-like structure With a single root directory at the top and branches down to nested files and subdirectories You can access any other file or directory by using a pathname When the chain starts with the root directory, it’s called an absolute pathname When chain starts from the current working directory, it’s called a relative pathname © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 22 Accessing and Manipulating Files and Directories on Disk (2 of 5) To open files named myfile.txt in the child, parent, and sibling directories, where current is the current working directory, you could use relative pathnames: childFile = open("child/my file.txt", 'r') parentFile = open("../my file.txt", 'r') siblingFile = open("../sibling/my file.txt", 'r') Pathname Target Directory myfile.txt current child/myfile.txt child../myfile.txt parent../sibling/myfile.txt sibling © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 23 Accessing and Manipulating Files and Directories on Disk (3 of 5) When designing Python programs that interact with files, it’s a good idea to include error recovery For example, before attempting to open a file for input, you should check to see if file exists Function os.path.exists supports this checking Example: To print all of the names of files in the current working directory with a.py extension: import os currentDirectoryPath = o s.getcwd() listOfFileNames = os.listdir(currentDirectoryPath) for name in listofFileNames: if “.py” in name: print(name) © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 24 Accessing and Manipulating Files and Directories on Disk (4 of 5) os Module Function ’ What it Does chdir(path) Changes the current working directory to path. getcwd() Returns the path of the current working directory. list dir(path) Returns a list of the names in directory named path. mkdir(path) Creates a new directory named path and places it in the current working directory. remove(path) Removes the file named path from the current working directory. rename(old, new) Renames the file or directory named old to new. rmdir(path) Removes the directory named path from the current working directory. sep A variable that holds the separator character (‘/’ or ‘\’) of the current file system. © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 25 Accessing and Manipulating Files and Directories on Disk (5 of 5) os.path Module What it Does Function exists(path) Returns True if path exists and False otherwise. isdir(path) Returns True if path names a directory and False otherwise. isfile(path) Returns True if path names a file and False otherwise. getsize(path) Returns the size of the object names by path in bytes. normcase(path) Converts path to a pathname appropriate for the current file system; for example, converts forward slashes to backslashes and letters to lowercase on a Windows system. © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 26 Chapter Summary (1 of 2) A string is a sequence of zero or more characters The len function returns the number of characters in its string argument A string is an immutable data structure The subscript operator[] can be used to access a character at a given position - Can also be used for slicing ([:]) in operator is used to detect the presence or absence of a substring in a string Method: operation that is used with an object The string type includes many useful methods for use with string objects © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 27 Chapter Summary (2 of 2) A text file is a software object that allows a program to transfer data to and from permanent storage A file object is used to open a connection to a text file for input or output Some useful methods: read, write, readline for loop treats an input file as a sequence of lines On each pass through the loop, the loop’s variable is bound to a line of text read from the file © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 28