Reputation: 3205
I am reading in a text file and printing the contents, but am getting the below result, instead of line breaks, etc, which when opening the text file I can see, it is printing out \n along with other characters. How do I change the printing to reflect what the text file actually looks like? This is how I am reading in the text file:
infile = file("/path/to/log/file")
line = infile.readlines()
print line
This is the print I am getting:
'[2014-02-12 11:24:14 GMT] INFO: Logging configured successfully.\n', '[2014-02-12 11:24:14 GMT] INFO: iTMSTransporter : iTunes Store Transporter [1.7.9]\n', '[2014-02-12 11:24:14 GMT] INFO: OS identifier: Mac OS X 10.9.1 (x86_64); jvm=24.0-b26; jre=1.7.0-internal-root_2012_12_05_20_29-b00\n', '[2014-02-12 11:24:14 GMT] INFO: Memory: [JVM] 20M free, 31M total, 989M max [System] (Physical) 824M free, 8192M total (Swap) 1024M free, 1024M total\n', '[2014-02-12 11:24:14 GMT]
Upvotes: 0
Views: 89
Reputation: 1
If you want read file and close it:
with open("/path/to/log/file") as file_:
for line in file_:
print line.strip()
Upvotes: 0
Reputation: 121975
The '\n'
character is the line break. str.strip()
will remove this and other whitespace for you:
for l in line:
print(l.strip())
Upvotes: 0
Reputation: 1394
Just do
for line in infile:
print line
What you were doing, calling infile.readlines(), gives you a list of all lines in the file.
Upvotes: 1