I want to understand how to calculate the number of integers per line. I have the following so far:
import sys
filename = raw_input("Enter Filename: ")
names_list = []
with open(filename, 'r') as file:
for line in file:
line = line.strip()
if line:
if not line.startswith("#"):
names_list.append(line)
with open(filename, 'r') as file:
for line in file.readlines():
words = len(line.split(' '))
print words
Output is:
Enter Filename: somenumbers.txt
6
9
4
1
5
5
5
5
1
5
1
1
5
20
Ouput should be:
Enter Filename: somenumbers.txt
9
4
5
5
5
1
20
Any suggestions of what I am doing incorrect to calculate the number of integers per line? Thank you.
EDIT: With the change to strip(), the output is incorrect. It appears that starting from the output of 6, every second integer is not suppose to print. Where are these additional values (a 6, 1, 5, 5, 5) coming from? How can I avoid these additional incorrect values? (Thank you all so far)