0

I'm not sure what I'm doing wrong here, spent all day googling and reading python books..

I have the following function:

def extract(inNo, inputFile2, outputFile):
    ifile = open(inputFile2, 'r')
    ofile = open(outputFile, 'w')
    lines = ifile.readlines()
    for line in lines:
        print(str(len(line)))
        if str(len(line)) == str(inNo):
            ofile.write(line)

I'm trying to understand len(), I seem to get odd results when using it. My input file is the following:

1
22
333
4444
55555
666666
7777777
88888888

Now if I use '7' as the inNo variable, the output (i.e., print) I get is:

2
3
4
5
6
7
8
8

and the output file becomes:

666666

I'm sure from checking in python.exe length count start from 1 i.e:

len('123') 

would give a result of

3

Is my understanding of len() wrong, or am I coding it wrong?

Essentially, what this function does is; it takes an input, an output and a character length as arguments. These come from a different function and the 2nd function calls this one with arguments.

This function reads lines from the input file. For every line, it gets the character length and compares it to the input No. If they are equal it should write that line to the output file. I assume as I have no "else:" it should carry on the iteration. The print() function allows me to see exactly what it has calculated as the length.

When I try to add '- 1' or '+ 1' to the len() i.e. (len(line) + 1) it causes even more odd things start to happen.

Ma0
  • 15,057
  • 4
  • 35
  • 65
  • 2
    `len(123)` is not 3. `len(123)` raises a TypeError, as integers do not have a length. – user2357112 Nov 22 '17 at 17:12
  • 6
    Each line in your input file ends with a newline character. Python's readlines() function does not remove these line endings. Therefore each line's length is `charactersYouCanSee + 1`, the line `666666` is really `666666\n` (`\n` is a single character) –  Nov 22 '17 at 17:14
  • also keep in mind that there is a `'\n'` character on every line. Use an editor that shows line breaks – Ma0 Nov 22 '17 at 17:14
  • 1
    Possible duplicate of [Reading a file without newlines](https://stackoverflow.com/questions/12330522/reading-a-file-without-newlines) – Josh Lee Nov 22 '17 at 17:15
  • @user2357112 i get the following from python, if i am wrong exp-lain and bring me evidence - Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 17:26:49) [MSC v.1900 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> len('123') 3 >>> – Bilal Ahmed Awan Nov 22 '17 at 17:17
  • 1
    @BilalAhmedAwan: See those quotation marks you typed in? Those matter. – user2357112 Nov 22 '17 at 17:17
  • You are also unnecessarily converting to `str()` at several places in your code. Write `print(len(line))` instead of `print(str(len(line)))` and write `if len(line) == inNo:` instead of `if str(len(line)) == str(inNo):`. – BioGeek Nov 22 '17 at 17:23
  • ahhh, I've has this problem before, i fixed it with strip('\n') i'll give it go tomorrow and @JoshLee yes that post is very helpful – Bilal Ahmed Awan Nov 22 '17 at 17:24
  • @user2357112, your initial comment could have been more clear. There's no reason to roundabout something as simple as "you have a typo". – Phillip Martin Nov 22 '17 at 17:29
  • 1
    @PhillipMartin: Seems more like a lack of understanding of data types than a typo to me. – user2357112 Nov 22 '17 at 17:30

1 Answers1

4

len() also considers the new line character \n, that's why you're getting one more in every line except the last one.