Python Programming Exercises 5

Python Programming Exercises 5

Notes: In these exercises we are going to learn about reading and writing files. The files referenced in the questions can be found on the course webpage.

1. Here is some boilerplate code to read a file and print out the contents to the screen: f = open("table.txt") for line in f : print(line.strip()) f.close() The strip() method used in the example code removes whitespace from the begining and end of a string: >>> " ".strip() '' Write this code to a file and test it out.

1

2. We can read a single line in a file by calling the readline() method on a file object. This is useful if we want to skip, for example, the first line in the file: f = open("table.txt") header = f.readline() print("first line of file:", header.strip()) for line in f : print(line.strip()) f.close()

3. We can turn a string into a list of tokens by calling the split() method. By default, tokens are defined as anything separated by whitespace (space, tab, new line, etc), but split() also accepts an optional argument specifying a different delimiter. For example: >>> "a,b,c,d,e".split(",") # comma delimiter >>> "a b c d e".split() # whitespace delimiter >>> "a b c d e".split()

4. Write a script to read in the file table.txt and print out just the fourth column.

5. Write a script to read in the file table.txt and print out the total of each row.

6. Write a script to read in the file table.txt and print out the total of each column.

2

7. Here is some boilerplate code to write to a file: f = open("write_test.txt", "w") f.write("this is a test\n") f.close() Line 1: To write to a file you first have to open it in write mode (the second argument does this). Line 2: Files are written using the write(? ? ?) method. You can only write strings to files and you need to explicitly add new lines (the "\n" character). Line 3: The changes are not necessarily written to disk until the file is closed. Run this code and open the file called write test.txt in a text editor to demonstate that it works.

8. As we can only write strings to files, we need an easier way to create strings using other variables. Previously you might have done this: x=7 y=3 print("x = " + str(x) + ", y = " + str(y)) Explicit conversions to string types and adding everything together can be error prone and does not look very neat. Instead we can use the string type's format(? ? ?) method to do the same thing: print("x = {}, y = {}".format(x, y))

The substring "{}" is a placeholder for where the converted value of the variable will be used. There are several different ways you can use format(? ? ?), see if you are interested.

3

9. This function counts the number of k-mers in a string containing DNA: def kmer(dna, k) : tmp = {}

for i in range(len(dna) - k + 1) : kmer = dna[i:i+k] if kmer not in tmp : tmp[kmer] = 0

tmp[kmer] += 1

return tmp

print(kmer("ATTCACCGGGAGGCTCCAGAGGCGTAGGCAGAGGATCCGAGAAAGGAGCGAGGGGAGTCA", 5)) Write a two column table of k-mers and counts to a file using the string format(? ? ?) method.

10. Write a function to remove punctuation from a string. Hint 1: The string module contains a string variable called punctuation which contains all punctuation characters. >>> import string >>> string.punctuation Hint 2: The string type has a method called replace(old, new) that you can use to remove a character: >>> s = "hello. world" >>> s.replace(".", "")

11. Use your answer from the previous question to write a script that reads a file and writes its contents to another file with all the punctuation removed. The new file should have a different name from the file being read.

4

12. How many times is the word "countenance" used in the novel Pride and Prejudice? Hint 1: one way to do this might be to use the count() method on a string: >>> s = "fundamental function funny fun funk fungi" >>> s.count("fun") Hint 2: another approach might be to use the split() method like in question 3. Hint 3: you can convert all characters to lower case using the lower() method on a string: >>> "HELLO".lower()

13. What is the most frequently occurring word in the novel Pride and Prejudice and how many times does it occur?

5

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download