-1

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)

  • 1
    Why do you open the file, process it into `names_list`, then immediately open the file for processing again and never use `names_list`? Since the actual and expected output don't even have the same number of lines, there's some other problem besides `strip` instead of `split` here. – TigerhawkT3 Jan 24 '16 at 04:56
  • @TigerhawkT3 Yes indeed..will change that. Thank you, I just started teaching myself Python today. – Justin_Finland Jan 24 '16 at 05:03
  • Please make a greater research effort before posting a question. – TigerhawkT3 Jan 24 '16 at 05:08

3 Answers3

1

Update:
You loop through the file's contents twice:

with open(filename, 'r') as file:
    #...  

with open(filename, 'r') as file:
    #...

The first time you add all the "valid" lines to an array, namely names_list (why this identifier?). The second time you just print the number of words in each line, regardless of their validity. You only need to loop once.

with open(filename, 'r') as file:
    for line in file:
        if line.strip() and not line.startswith("#"):
            names_list.append(line)
            print "There are ", len(line.split()), " numbers on this line"

What you want is split.

See this answer.

Assuming that the line contains only integers on each non-# line, just split it and count the number of "words".

#after checking the line is valid
print "This line contains", len(line.split()), "numbers"
Community
  • 1
  • 1
Arc676
  • 4,445
  • 3
  • 28
  • 44
  • Just corrected that. Yes, that assumption is right. Thanks. My output is still incorrect. Please see my update, sir. – Justin_Finland Jan 24 '16 at 04:52
  • Wonderful. Thank you. I have one more question though. With your update I am printing out lines that do not have any values on them because there are three empty lines in the file. How can I combat against printing out those integers for the empty lines? I thought something like: `if not line.startswith("\n")`. Any suggestions, please? – Justin_Finland Jan 24 '16 at 05:02
  • I edited again: putting back `line.strip()` makes the string return `False` if it's empty. Alternatively, you can use `if` to only print when the length is greater than 0. – Arc676 Jan 24 '16 at 05:05
  • Thank you! I appreciate your help, sir. – Justin_Finland Jan 24 '16 at 05:06
1

Use words = len(line.split(' ')) instead of words = len(line.strip(' ')).

Update:

Instead of the second opening of the file, use: for name in names_list:

Update2:

You can simplify this further with:

with open(filename, 'r') as file:
    for line in file:
        print(len(line.strip().split()))
Mark
  • 309
  • 1
  • 9
0

I think you meant .split() instead of .strip(). Replace:

words = len(line.strip(' '))

with:

words = len(line.split())
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195