-
Notifications
You must be signed in to change notification settings - Fork 0
/
numlines1.py
80 lines (65 loc) · 1.95 KB
/
numlines1.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
71
72
73
74
75
76
77
78
79
80
# Files: numlines.py
# Author: Simon Chu
# Date: Apr. 19, 2017
# Purpose: read a file, make a exact copy
# except for numbering each line and count
# the number of different letters
def intro():
print()
print("Program to number lines in a file.")
print("You will enter the name of a file.")
print("The program will create an output file.")
print("Written by Simon Chu.")
print()
def getInput():
filename = input("Enter name of input file: ")
print() # for turnin
outFilename = filename.split('.')[0] + ".out"
print("The name of output file is: " + outFilename)
print()
return filename, outFilename
def fileProcess(filename,outFilename):
infile = open(filename, "r").read().split('\n')
num = 1
outfileContent = ''
output = ''
for line in infile:
if line != '':
output = " " + str(num) + ": " + infile[num - 1] + '\n'
num = num + 1
outfileContent = outfileContent + output
outfile = open(outFilename, "w")
outfile.write(outfileContent)
outfile.close()
return infile, num
def countLetters(infile):
line = ''
for i in infile:
line = line + i
j = 65
k = 97
list = ['0'] * 26 # initialize the value of the list
print(line.strip())
for ch in line.strip():
l = ord(ch)
for m in range(j,j + 26):
if m == l:
list[m - 65] = str(int(list[m - 65]) + 1)
for n in range(k,k + 26):
if n == l:
list[n - 97] = str(int(list[n - 97]) + 1)
return list
def printResult(list,num):
print("Number of lines:{0:>13}".format(num - 1))
x = 65
for y in range(0, 26):
print("Number of", chr(x) + "'s:{0:>15}".format(int(list[y])))
x = x + 1
def main():
intro()
filename, outFilename = getInput()
infile, num = fileProcess(filename,outFilename)
list = countLetters(infile)
printResult(list,num)
print()
main()