File tree 1 file changed +36
-0
lines changed
1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Takes a column from a file and calculates its frequency in the column. #prints the item and frequency in the output file.
2
+ # sorts the output file by highest frequency
3
+
4
+ infile = '/Users/rsn13/Desktop/scripts/input/pubs.csv'
5
+ outfile = '/Users/rsn13/Desktop/scripts/output/freq.txt'
6
+
7
+ with open (infile ) as file :
8
+ items = []
9
+ frequency = {}
10
+
11
+ for line in file :
12
+ line = line .strip ()
13
+ items .append (line )
14
+
15
+ for item in items :
16
+ if item in frequency :
17
+ frequency [item ] += 1
18
+ else :
19
+ frequency [item ] = 1
20
+
21
+ #print(frequency)
22
+ sort_orders = sorted (frequency .items (), key = lambda x : x [1 ], reverse = True )
23
+
24
+ sourceFile = open (outfile ,'w' )
25
+ for i in sort_orders :
26
+ print (i [0 ], i [1 ], file = sourceFile )
27
+
28
+ sourceFile .close ()
29
+
30
+ print ('Done' )
31
+
32
+
33
+
34
+
35
+
36
+
You can’t perform that action at this time.
0 commit comments