-2

I'm reading this lines from a file

yara_rule1:
   rule match:
   problem:

yara_rule2:
   rule match:
   problem:

when I print it into console the spaces before "rule match" and "problem" are omitted . what is the problem

input_data = open(file)
for line in input_data:
    print line.strip()
user3832061
  • 477
  • 3
  • 7
  • 12
  • 1
    Please copy/paste the output from the console into your question. Also removing surrounding whitespace is exactly what `strip()` does - in fact, that's the only reason I can imagine you would use it. – TheSoundDefense Aug 03 '14 at 14:04
  • 1
    Wow. You're `strip()`ing them. – vaultah Aug 03 '14 at 14:05

1 Answers1

1

str.strip() removes all whitespace from both the end and start of the string. In other words, it is the line.strip() method call that produces a line without the initial whitespace.

If you wanted to remove just the newline, use str.rstrip():

print line.rstrip('\n')

Compare:

>>> '   rule match:\n'.strip()
'rule match:'
>>> '   rule match:\n'.rstrip('\n')
'   rule match:'
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343