-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.py
71 lines (60 loc) · 1.79 KB
/
string.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
word = 'bananana'
i = word.find("na")
print(i) # 2 the index number of position if not find will return -1
# Slicing Strings
s = "Monty Python"
print(s[0:4]) # first 4 index
# String Concatenation
# using + same syntex as JavaScrip
print('Hello ' + s)
# split() made a list of string. split look for space to split.
abc = 'With three words'
stuff = abc.split()
print(stuff)
# more example
line = 'His e-mail is [email protected]'
pieces = line.split() # split by emtry space
parts = pieces[3].split('-') # split at '-'
print(parts[1])
counts = {'quincy':1, 'mrugest': 42, 'beau' : 100}
print(counts.get('kris', 0))
print(counts)
# Parsing and Extracting: this is an example
data = 'From [email protected] Sat Jan 5 09:14:16 2021'
beginpos = data.find('@')
endpos = data.find(' ',beginpos) #looking for space ' '
host = data[beginpos+1 : endpos]
print(host)
# NOTE in python 3, all string are Unicode (take foreige languge or non-latin charactor as String type)
# Reading files
# fhand = open('app.py')
# for line in fhand:
# line = line.rsplit() # to avoid white space
# if line.startswith('print'):
# print(line)
# # Skipping with continue
# fhand = open('mbox-short.txt')
# for line in fhand:
# line = line.rsplit()
# if not line.startswith('From:'):
# continue
# print(line)
# Using in to select lines
# fhand = open('mbox-short.txt')
# for line in fhand:
# line = line.rsplit()
# if not '@uct.ac.za' in line :
# continue
# print(line)
# Prompt for file name input
fname = input('Enter the file name: ')
try:
fhand = open(fname)
except:
print('File cannot be opened:', fname)
quit()
count = 0
for line in fhand:
if line.startswith('Subject:') :
count += 1
print('There were', count, 'subject lines in' , fname)