0

I'm trying to figure out how to process an input file and print it out without any whitespace present in the output.

My current script looks like this:

#!/usr/bin/python

kf = open ('keyword.txt', 'r')
sl = open ('syslog.txt', 'r')

for line in kf:
    line.replace('\n', "")
    line.strip()
    print line  

The output of this however looks like this:

polkitd

anacron

acpid

rt-kit daemon

goa

AptDaemon

AptDaemon.PackageKit

AptDaemon.Worker

python

Does anyone know how to remove the lines of whitespace between each item?

Sam Hanley
  • 4,707
  • 7
  • 35
  • 63
Joanna
  • 11
  • 3

2 Answers2

0

Try printing repr(line) instead of print line and see what you get. If there is a '\n', try calling line.strip('\n'). Also, try putting a comma , after print line as such print line,.

A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
0

both replace and strip return new objects, not change the one you are currently working with so your code should be something like this

line = line.replace(...)
line = line.strip(...)
Logan Murphy
  • 6,120
  • 3
  • 24
  • 42