|
| 1 | +# 1.file hande as a sequence |
| 2 | +handle = open('test.txt') |
| 3 | +for cheese in handle: |
| 4 | + print(cheese) |
| 5 | +# cheese means iteration variable, print out each line of file test.txt |
| 6 | +print('#####################################################') |
| 7 | +# 2.counting lines in a file |
| 8 | +handle = open('test.txt') |
| 9 | +count = 0 |
| 10 | +for line in handle: |
| 11 | + count = count+1 |
| 12 | +print('Line count:', count) |
| 13 | +# 2.counting lines in a file |
| 14 | +handle = open('mbox.txt') |
| 15 | +count = 0 |
| 16 | +for line in handle: |
| 17 | + count = count+1 |
| 18 | +print('Line count:', count) |
| 19 | +# "line" is just a random name of iteration variable! |
| 20 | +print('#####################################################') |
| 21 | +print('3.Reading the *whole* file') |
| 22 | +print('you can red the whole file (newline and all) ' |
| 23 | + 'into a single string') |
| 24 | +fhand = open('test.txt') |
| 25 | +ghand = open('test.txt', 'r') |
| 26 | +# you have to use string.read() to read whole thing into string, |
| 27 | +# also read newline as "\n" |
| 28 | +inp = fhand.read() |
| 29 | +print(len(inp)) # print the length of the file |
| 30 | +print(inp) |
| 31 | +print('fhand is', fhand) |
| 32 | +print('ghand is', ghand) |
| 33 | +# means 'r' in open doesn't read the file |
| 34 | +print(inp[:20]) # print out the first 20 letters |
| 35 | +# (not included the 20th character!) |
| 36 | +print('#####################################################') |
| 37 | +# 4.Search through a file |
| 38 | +print('put an "if" statement in "for" loop ' |
| 39 | + 'to only print out certain lines') |
| 40 | +fhand = open('mbox_short.txt') |
| 41 | +for line in fhand: |
| 42 | + if line.startswith('From: '): |
| 43 | + print(line) |
| 44 | +# already checked, all lines meet the criteria are printed out |
| 45 | +# Problem: lots of blank line between each line. |
| 46 | +# Reason: 1. each line from the file has a "newline" at the end |
| 47 | +# 2. "print" statement add "newline" to each line |
| 48 | +#Solution: strip the whitespace from |
| 49 | +# the right-hand side of the string using "rstrip()" |
| 50 | +# newline is considered as whitespace |
| 51 | +print('fixed version: strip newline:') |
| 52 | +fhand = open('mbox_short.txt') |
| 53 | +for line in fhand: |
| 54 | + if line.startswith('From: '): |
| 55 | + print(line.rstrip()) |
| 56 | +# or print('fixed version: strip newline:') |
| 57 | +# fhand = open('mbox_short.txt') |
| 58 | +# for line in fhand: |
| 59 | +# line = line.rstrip() <--------------------- |
| 60 | +# if line.startswith('From: '): |
| 61 | +# print(line) |
| 62 | +print('#####################################################') |
| 63 | +print('4.skipping with continue') |
| 64 | +print('we can conveniently skip a line by ' |
| 65 | + 'using "continue" statement') |
| 66 | +fhand = open('test.txt') |
| 67 | +for line in fhand: |
| 68 | + line = line.rstrip() |
| 69 | + if line.startswith('From: '): |
| 70 | + continue |
| 71 | + print(line) |
| 72 | +print('*******************') |
| 73 | +fhand = open('test.txt') |
| 74 | +for line in fhand: |
| 75 | + line = line.rstrip() |
| 76 | + # Skip 'uninteresting lines' |
| 77 | + if not line.startswith('From:'): |
| 78 | + continue |
| 79 | + # Process our 'interesting' line |
| 80 | + print(line) |
| 81 | +print('#####################################################') |
| 82 | +print('5. Usnig "in" to select lines') |
| 83 | +print('we can find a string in a line as our selection criteria') |
| 84 | +fhand = open('test.txt') |
| 85 | +for line in fhand: |
| 86 | + line = line.rstrip() |
| 87 | + if not '@uct.ac.za' in line: |
| 88 | + continue |
| 89 | + print(line) |
| 90 | +print('**********************') |
| 91 | +fhand = open('test.txt') |
| 92 | +for line in fhand: |
| 93 | + line = line.rstrip() |
| 94 | + if line.find('@uct.ac.za')== -1: |
| 95 | + continue |
| 96 | + print(line) |
| 97 | + print(line.find('@uct.ac.za')) |
| 98 | + #'find' statement: either returns the position of the string |
| 99 | + # or -1 if the string was not found |
| 100 | +print('#####################################################') |
| 101 | +print('6.promp with file name') |
| 102 | +fname = input('Enter the file name: ') |
| 103 | +try: |
| 104 | + fhand = open(fname) |
| 105 | +except: |
| 106 | + print('File cannot be opened:', fname) |
| 107 | + exit() |
| 108 | +subject = input('we gonna find: ') |
| 109 | +count =0 |
| 110 | +for line in fhand: |
| 111 | + line = line.rstrip() |
| 112 | + if line.startswith(subject): |
| 113 | + count = count+1 |
| 114 | +print('there are', count, 'subject lines in', fname ) |
| 115 | +print('#####################################################') |
0 commit comments