1

I have a simple .txt file with bunch of lines for instance

motorola phone
happy cows
teaching
school work
far far north
teaching
hello

now all I want to do is read all these strings and print out. So if the line contains teaching I want to print teaching is awesome so here is my code

with open("input.txt", "r") as fo:
    for line in fo:
        if "teaching" in line:
            line = line.rstrip('\n') + " is awesome"
            print line
        else:
            print(line.rstrip('\n'))

But this is printing

enter image description here

so what is happening to the rest of the string. Because it is suppose to print teaching is awesome isn't it. Can some one explain this behaviour of python. Thanks

Shadid
  • 4,133
  • 9
  • 30
  • 53

3 Answers3

1
rstrip('\r\n') 

works like a charm. Must mention I'm on a Ubuntu but it works in windows only with '\n' .

Shadid
  • 4,133
  • 9
  • 30
  • 53
0

You probably have a windows file with '\r\n' and rstrip is returning \r which means the carriage returns to the beggining of the line and being overwritten.

Try rstrip('\r').rstrip('\n')

RedX
  • 14,749
  • 1
  • 53
  • 76
  • Or just `rstrip()`. I'll never understand why people limit the stripping to just one kind of space without any apparent reason. More code and more error-prone... – Stefan Pochmann Jan 24 '17 at 16:15
  • Actually, you got the order wrong. For `'\r\n'` you'd need to strip `\n` *first*. More error-prone indeed... – Stefan Pochmann Jan 24 '17 at 16:16
  • 1
    there's no reason to call it twice - `.rstrip('\r\n')` does the same thing (as does `.rstrip('\n\r')`). The characters given to strip/rstrip are treated as a set of possible characters to remove. – KingRadical Jan 24 '17 at 18:09
  • @KingRadical That does *not* do the same thing, as it actually works :-) – Stefan Pochmann Jan 27 '17 at 01:53
  • @KingRadical As far as I can see, you're just wrong there. But feel free to elaborate if you disagree. – Stefan Pochmann Jan 30 '17 at 20:16
  • The result of `.rstrip('\r\n')` is the same as the result of `.rstrip('\n').rstrip('\r')` is the same as the result of `.rstrip('\n\r')`: `In [9]: a Out[9]: 'abcd\r\n' In [10]: a.rstrip('\n').rstrip('\r') Out[10]: 'abcd' In [11]: a.rstrip('\r\n') Out[11]: 'abcd' In [12]: a.rstrip('\n\r') Out[12]: 'abcd'` – KingRadical Jan 30 '17 at 20:21
  • @KingRadical Yeah but the answer says `rstrip('\r').rstrip('\n')`. Not `rstrip('\n').rstrip('\r')`. You just made that up. – Stefan Pochmann Jan 30 '17 at 20:29
  • That was in reference to your comment about stripping `'\n'` first, but okay – KingRadical Jan 30 '17 at 20:39
  • @KingRadical Then it should've addressed me. Also, I had already argued against stripping just one kind of space (at a time) and suggested to do it at once. I said they **would** need to strip `\n` first (if they did it in two steps at all, which was clearly *not* my suggestion). Finally, your `.rstrip('\r\n')` doesn't do the same as `.rstrip('\n').rstrip('\r')`, either, as for example the string `'foo\n\r'` shows. – Stefan Pochmann Jan 30 '17 at 20:59
0

To me it works too... but I am using python 3, so I put the round brackets after print... but you used in line 7 and not in line 5... ?!

with open("input.txt", "r") as fo:
    for line in fo:
        if "teaching" in line:
            line = line.rstrip('\n') + " is awesome"
            print(line)
        else:
            print(line.rstrip('\n'))

and this is the output:

motorola phone
happy cows
teaching is awesome
school work
far far north
teaching is awesome
hello
>>> 
PythonProgrammi
  • 22,305
  • 3
  • 41
  • 34