-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path09_files.py
More file actions
147 lines (102 loc) · 3.56 KB
/
09_files.py
File metadata and controls
147 lines (102 loc) · 3.56 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import csv
import string
def get_points ( file_path ):
points = []
file = open ( file_path )
for line in file :
l = line . split ()
points . append (l)
return points
points_list = get_points ('points.txt')
print ( points_list )
########################################################################################################################
def read_csv_file ( file_path ):
data = []
file = open ( file_path )
csv_reader = csv. reader ( file )
for row in csv_reader :
data . append (row)
return data
def list_gps_commands ( data ):
dictionary = dict ()
for row in data :
gps_cmd = row [0]
if gps_cmd in dictionary :
dictionary [ gps_cmd ] += 1
else :
dictionary [ gps_cmd ] = 1
return dictionary
gps_list = read_csv_file (r'GPS.csv')
gps_dict = list_gps_commands ( gps_list )
print ( gps_dict )
########################################################################################################################
def word_count ( file_path ):
file = open ( file_path )
dictionary = {}
for line in file:
line = line.strip(string.whitespace +
string.punctuation).lower().split()
for word in line:
if word in dictionary:
dictionary[word] += 1
else:
dictionary[word] = 1
file.close
return dictionary
word_table = word_count ('snark.txt')
print ( word_table ["and"])
########################################################################################################################
def store_dict ( dictionary , file_path ):
file = open ( file_path , "w")
for key in dictionary :
file . write ("%s: %i\n" %( key , dictionary [key ]))
file.close ()
store_dict (wordtable, "result32.txt")
########################################################################################################################
word_table = word_count ('snark.txt')
def most_often ( dictionary ):
count = 0
word = ''
for key in dictionary :
if dictionary [key ] > count :
count = dictionary [key]
word = key
return word , count
print ( most_often ( word_table ))
def most_often_5_chars ( dictionary ):
count = 0
word = ''
for key in dictionary :
if len(key) == 5:
if dictionary [key ] > count :
count = dictionary [key]
word = key
return word , count
print ( most_often_5_chars ( word_table ))
def most_often_n_chars ( dictionary , length ):
count = 0
word = ''
for key in dictionary :
if len(key) == length :
if dictionary [key ] > count :
count = dictionary [key]
word = key
return word , count
print ( most_often_n_chars ( word_table , 5))
#######################################################################################################################
def begin_with_b ( file_path ):
word_table = word_count ( file_path )
count = 0
for key in word_table :
if key [0] == 'b':
count += 1
return count
print ( begin_with_b ('snark.txt'))
def begin_with_char ( file_path , char ):
word_table = word_count ( filename )
count = 0
for key in word_table :
if key [0] == char :
count += 1
return count
print ( begin_with_char ('snark.txt', 'b'))