-
Notifications
You must be signed in to change notification settings - Fork 0
deequez/Exercise05
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Exercise 05: Files, Creative uses of lists ========================================== Introduction ------------ Files can be opened using the .open method, and then processed one character at a time as a bytestream using the .read method with an argument of 1: f = open("myfile.txt") firstletter = f.read(1) f.close() The entire file can be read in all at once using the .read method with no arguments: f = open("myfile.txt") filetext = f.read() f.close() Once a file has been read, it can be iterated through as if it were a list: # Print one character at a time to the screen for char in filetext: print char Resources: * http://learnpythonthehardway.org/book/ex15.html * http://learnpythonthehardway.org/book/ex16.html * http://learnpythonthehardway.org/book/ex17.html * http://www.asciitable.com/ * http://docs.python.org/library/functions.html#ord Problem Description ------------------- Write a program, lettercount.py, that opens a file named on the command line and counts how many times each letter occurs in that file. Your program should then print those counts to the screen, in alphabetical order. For example: inputfile.txt: An abacus $ python lettercount.py inputfile.txt 3 1 1 0 0 ... Once you've produced appropriate output, you can visualize it by piping the contents of your program to the 'spark' utility installed on your computer: $ python lettercount.py inputfile.txt | spark ▃▂▄▁▇▅█▁▇▂▁▁▅▃▅▇▁▄▄▂▂▇▆▄▂▄ You may find the following two methods useful: string.lower() ord() We have provided a file 'twain.txt' for you to test your code on.
About
Files, Creative uses of lists
Resources
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published