Date post: | 23-Dec-2015 |
Category: |
Documents |
Upload: | sabrina-robbins |
View: | 223 times |
Download: | 0 times |
Lecture 17 – Processing the Content of Text Files
COMPSCI 101Principles of Programming
2COMPSCI 101 - Principles of Programming
At the end of this lecture, students should be able to: read the content of a text file into a list obtain, process, and update the data inside the file use the split function to divid a string into different parts write the updated content back to a text file
Learning outcomes
3COMPSCI 101 - Principles of Programming
RecapThe copy_file function copies the content of a source
file into a target file.def copy_file(file1, file2): infile = open(file1, "r") content = infile.read() infile.close()
outfile = open(file2, "w") outfile.write(content) outfile.close()
return len(content)
def main(): length = copy_file("stock.txt", "stock2.txt") print("File copied, size is " + str(length) + " characters.")
main() File copied, size is 314 characters.
BC001,Fresh toast bread white (700g),3.99,20BC002,Low-fat milk (2 liter),4.80,10BC003,V-energy drink,2.75,10BC004,Fresh garlic (450g),1.98,0BC005,Coca-Cola (300 ml),2.50,10BC006,Pineapple,3.60,6BC007,Mango,1.89,4BC008,Snickers chocolate bar,1.80,20BC009,Broccoli,1.47,11BC010,Washed Potato (2.5kg),2.98,7
4COMPSCI 101 - Principles of Programming
Counting the number of words in a text file. Open the file and read the content of file as a string Split the content into words in a list Return the length of the words list
Example – Counting Words
def count_words(filename): infile = open(filename, "r") content_str = infile.read() infile.close() words_list = content_str.split() return len(words_list)
def main(): length = count_words("words.txt") print("There are " + str(length) + " words in the file.")
main() There are 27 words in the file.
The woods are lovely, dark, and deep,But I have promises to keep,And miles to go before I sleep,And miles to go before I sleep.
5COMPSCI 101 - Principles of Programming
The ‘stock.txt’ file contains information about the items on sale in a simplified online shopping system. Each line contains the information about an item on sale, such as the
barcode of the item, the description, the price and the quantity (of how many available).
During a shopping scenario, an user can: Place an item in the shopping cart by selecting the item code, Remove an item from the shopping car by its barcode, Check out the shopping cart, which will result in a bill being generated.
Example - Online Shopping
BC001,Fresh toast bread white (700g),3.99,20BC002,Low-fat milk (2 liter),4.80,10BC003,V-energy drink,2.75,9BC004,Fresh garlic (450g),1.98,0BC005,Coca-Cola (300 ml),2.50,10BC006,Pineapple,3.60,6BC007,Mango,1.89,4BC008,Snickers chocolate bar,1.80,20BC009,Broccoli,1.47,11BC010,Washed Potato (2.5kg),2.98,7
6COMPSCI 101 - Principles of Programming
Given an item code, can we find out whether the item represented by the code exists in the stock file or not?Read in content of the stock file, and use the split function to
break up the content into a list of item records. Search through the list of item records to check whether the given
item code exists. Return the index if found, or -1 if not found.
Finding an Item
def load_stock(filename): infile = open(filename, "r") stock_str = infile.read() infile.close() stock_list = stock_str.split("\n”) return stock_list
def find_item(items_list, code): # Add your code here
def main(): items_list = load_stock("stock.txt") barcode = input("Enter item code: ") index = find_item(items_list, barcode) if index != -1: print(items_list[index]) else: print("The item does not exist.")
main()Enter item code: BC006BC006,Pineapple,3.60,6
7COMPSCI 101 - Principles of Programming
Each item record in the list contains the code, description, price and quantity, which are separated by the comma.Use the split function on the item string, a list of item attributes can
be obtained, e.g., item_list[0] contains the item code, etc.The searching can be based on matching the item code of each item record against the given code using a loop.The loop stops as soon as a match code is found. Thus, a while loop would be better, since the repetition varies.
Finding an Item
def find_item(items_list, code): index = 0 found = False while (not found and index < len(items_list)): item = items_list[index] item_list = item.split(",") if item_list[0] == code: found = True index = index +1 if found: return index-1 else: return -1
8COMPSCI 101 - Principles of Programming
Can we sum up the total cost of all the items on sale, which are stored in the stock file?Read in content of the stock file, and use the split function to
break up the content into a list of item records.Traverse through each item in the list and obtain the unit price
and its quantity, then add the multiplication to the sum of costs.
Summing up the total
def load_stock(filename): # details omitted
def sum_items(items_list): sum = 0 for item in items_list: item_list = item.split(",") cost = float(item_list[2])*int(item_list[3]) sum = sum + cost return sum
def main(): items_list = load_stock("stock.txt") total_cost = sum_items(items_list) print("The total cost is: $" \ + format(total_cost, ".2f"))
main()
The total cost is: $552.29
9COMPSCI 101 - Principles of Programming
When an item is added to or removed from the shopping cart, its quantity value needs to be updated in the stock.Complete the update_quantity function below, which updates the
quantity value of an item by an integer (either positive or negative).
Exercise
def main(): items_list = load_stock("stock.txt") barcode = input("Enter item code: ") value = int(input("Enter value update: ")) index = find_item(items_list, barcode) if index != -1: print("Old: " + items_list[index]) update_quantity(items_list, index, value) print("New: " + items_list[index]) else: print("The item does not exist.")
main()
Enter item code: BC004Enter value update: 25Old: BC004,Fresh garlic (450g),1.98,0New: BC004,Fresh garlic (450g),1.98,25
# details of load_stock and find_item omitted
def update_quantity(stock_list, index, value): item = stock_list[index] item_list = item.split(",") # Add your code here, where new value is # calculated, and item string is updated stock_list[index] = item
10COMPSCI 101 - Principles of Programming
We can write a function for adding an item into the shopping cart. Obtain user input of the code of the item to be added, and check whether
such an item exist in the stock list. If code exists, check whether it is out of stock or not. If all okay, add the item into the shopping cart, and decrease the quantity
value of the item in the stock by 1. Otherwise, report corresponding error messages.
We can write a function for removing an item from the shopping cart. Obtain input from user for the item code to be removed. Check whether such an item exists in the stock list and the shopping cart. If code exists, remove the item from the shopping cart, and and increase the
quantity value of the item in the stock by 1. Otherwise, report corresponding error messages.
Other Useful Functions
11COMPSCI 101 - Principles of Programming
SummaryThe read() method can read the entire content of a file as a
single string.With the help of the split function, you can break down the
content into a list of string values separated by certain symbol, e.g., lines separated by the return symbol “\n”.
For each string element in the above list, you can further break it down into a list containing smaller parts of the information when processing the data record.
The data obtained from a file is in a string format, you can cast it into corresponding types when processing the data.
After updated the content of file stored in the list, you can use the write() method to save the data back to a file.
12COMPSCI 101 - Principles of Programming
Opening a file f = open( filename[, mode] )Closing a file f.close()Reading a file f.read()Writing a file f.write( string )The split function item = "BC009,Broccoli,1.47,11"
item_list = item.split(",”) # item_list is ['BC009', 'Broccoli', '1.47', '11']
Python features used in this lecture