Contents |
[edit]
Make a file reader
Task 1 - Data files. Create two text files on your machine. These text files will be used as test data for worksheets in the future.
File 1 - One word a line Call this file words.txt The first text file should contain a list of words, with one word to a line. These words should not be alphabetically sorted, and you should have around 1000 words in the file. You should think about intelligent ways of producing this file, possibly using some UNIx command line facilities to produce a list of words.
File 2 - Sentences Call this file lines.txt The second text file should contain text with multiple words on a line, with multiple lines of text. There should not be any formatting other than newlines. You should include punctuation, and may want to derive your text from a copyright free e-book. (Project Gutenberg is a good source for these.)
Task 2 - A File Reader Using a java FileReader and BufferedReader read in the first 10 lines of words.txt and print them out on the screen. Ensure that you close the file cleanly after reading the first 10 lines.
[edit]
Solutions
file = open("words.txt",'r')
for each in range(10): print file.readline()
file.close()
[edit]
File Parser
Task 1 - A Simple File Parser Using a java FileReader and BufferedReader read in the first 20 lines of lines.txt. Print out the 1st word from the 1st line, the 2nd word from the 2nd line and so on. If the nth line has less than n words, then you should print out the last word on the line. You should look at the split method in the String class to help you with this task. Ensure that you close the file cleanly after reading the first 20 lines.
Task 2 - Parse Content of File Count the number of words in the first 200 sentences, and print out the first and last word from each sentence with the number of words between them. Sentences are terminated with a full stop, and this should not be included in the last word, when you print it out.
[edit]
Solutions
file = open("lines.txt",'r')
for each in range(20):
words = file.readline().split()
try:
print words[each]
except:
print words[-1]
file.close()
haven't done the second one yet