Reputation:
I am trying to count the number of times 1,2,3,...,9 appear at the beginning of each number in a file. This is how my code goes:
DECIMAL_NUM='123456789'
def main():
#get the file name from the user
file_name=str(input("Enter a file name: "))
#open the file to read
input_file= open(str(file_name),'r')
#reads the first line of the file
line=input_file.readline().strip()
one=0
two=0
three=0
four=0
five=0
six=0
seven=0
eight=0
nine=0
i=0
while line!="":
if line[0]==DECIMAL_NUM[0]:
one+=1
elif line[0]==DECIMAL_NUM[1]:
two+=1
elif line[0]==DECIMAL_NUM[2]:
three+=1
elif line[0]==DECIMAL_NUM[3]:
four+=1
elif line[0]==DECIMAL_NUM[4]:
five+=1
elif line[0]==DECIMAL_NUM[5]:
six+=1
elif line[0]==DECIMAL_NUM[6]:
seven+=1
elif line[0]==DECIMAL_NUM[7]:
eight+=1
elif line[0]==DECIMAL_NUM[8]:
nine+=1
line=input_file.readline().strip()
i+=1
input_file.close()
print(one)
print(two)
main()
I am also counting how many numbers are there in the file, so that I can calculate percentage of appearance of each digit. I think my codes are a little bit wordy and there might be a better way to do it. The input file has the following numbers:
1292
1076
188040
1579
3510
2597
3783
64690
For some reason, I am getting the number of times 1 is appearing as 1, when it should be 5. Could someone please give me some pointers? Thanks
Upvotes: 1
Views: 240
Reputation: 1095
You code is fine. It's your data file that's giving you problem. Remove the blank lines and your program should give you the right results.
1292
1076
188040
1579
3510
2597
3783
64690
After you processed the first line, the next line is read. But that's a blank line and your while loop ends.
Upvotes: 0
Reputation: 5280
Here is one way of approaching this task:
# Get non-empty lines from input file:
relevant_lines = [line for line in open(file_name).readlines() if line.strip()]
# Count them:
num_lines = len(relevant_lines)
import defaultdict
# If a key does not exist in a defaultdict when adding a value for it,
# it will be added with a default value for the given data type
# (0 in case of int):
d = defaultdict(int)
# Iterate through lines; get first character of line
# and increment counter for this character by one in defaultdict:
for line in relevant_lines:
d[line[0]] += 1
# Print results:
for key, value in d.items():
print(k + ' appears ' + value + ' times in file.')
If you are not allowed to use dict
s, here's how to fix your code:
DECIMAL_NUM='123456789'
def main():
# Get file name from user
file_name = input("Enter a file name: ")
# Open the file to read, and get a list of all lines:
lines = open(file_name, 'r').readlines()
one = 0
two = 0
three = 0
four = 0
five = 0
six = 0
seven = 0
eight = 0
nine = 0
for line in lines:
if line.strip(): # Check if line is not empty
if line[0] == DECIMAL_NUM[0]:
one += 1
elif line[0] == DECIMAL_NUM[1]:
two += 1
elif line[0] == DECIMAL_NUM[2]:
three += 1
elif line[0] == DECIMAL_NUM[3]:
four += 1
elif line[0] == DECIMAL_NUM[4]:
five += 1
elif line[0] == DECIMAL_NUM[5]:
six += 1
elif line[0] == DECIMAL_NUM[6]:
seven += 1
elif line[0] == DECIMAL_NUM[7]:
eight += 1
elif line[0] == DECIMAL_NUM[8]:
nine += 1
print(one)
print(two)
main()
Upvotes: 1