robintw
robintw

Reputation: 28531

Why are there extra blank lines in my python program output?

I'm not particularly experienced with python, so may be doing something silly below. I have the following program:

import os
import re
import linecache

LINENUMBER = 2

angles_file = open("d:/UserData/Robin Wilson/AlteredData/ncaveo/16-June/scan1_high/000/angles.txt")

lines = angles_file.readlines()

for line in lines:
    splitted_line = line.split(";")
    DN = float(linecache.getline(splitted_line[0], LINENUMBER))
    Zenith = splitted_line[2]
    output_file = open("d:/UserData/Robin Wilson/AlteredData/ncaveo/16-June/scan1_high/000/DNandZenith.txt", "a")
    output_file.write("0\t" + str(DN) + "\t" + Zenith + "\n")
    #print >> output_file, str(DN) + "\t" + Zenith
    #print DN, Zenith

output_file.close()

When I look at the output to the file I get the following:

0   105.5     0.0

0   104.125  18.0

0   104.0    36.0

0   104.625  54.0

0   104.25   72.0

0   104.0    90.0

0   104.75  108.0

0   104.125 126.0

0   104.875 144.0

0   104.375 162.0

0   104.125 180.0

Which is the right numbers, it just has blank lines between each line. I've tried and tried to remove them, but I can't seem to. What am I doing wrong?

Robin

Upvotes: 2

Views: 11661

Answers (5)

ojrac
ojrac

Reputation: 13421

EDIT: See comments for details, but there's definitely a better way. [:-1] isn't the best choice, no matter how cool it looks. Use line.rstrip('\n') instead.

The problem is that, unlike file_text.split('\n'), file.readlines() does not remove the \n from the end of each line of input. My default pattern for parsing lines of text goes like this:

with open(filename) as f:
    for line in f.readlines():
        parse_line(line[:-1]) # funny face trims the '\n'

Upvotes: 2

John Machin
John Machin

Reputation: 82934

For a GENERAL solution, remove the trailing newline from your INPUT:

splitted_line = line.rstrip("\n").split(";")

Removing the extraneous newline from your output "works" in this case but it's a kludge.

ALSO: (1) it's not a good idea to open your output file in the middle of a loop; do it once, otherwise you are just wasting resources. With a long enough loop, you will run out of file handles and crash (2) It's not a good idea to hard-wire file names like that, especially hidden in the middle of your script; try to make your scripts reusable.

Upvotes: 16

eduffy
eduffy

Reputation: 40224

If you want to make sure there's no whitespace on any of your tokens (not just the first and last), try this:

splitted_line = map (str.strip, line.split (';'))

Upvotes: 1

slovon
slovon

Reputation: 181

Alternative solution (handy if you are processing lines from file) is to strip the whitespace:

Zenith = Zenith.strip();

Upvotes: 2

Andrew Hare
Andrew Hare

Reputation: 351526

Change this:

output_file.write("0\t" + str(DN) + "\t" + Zenith + "\n")

to this:

output_file.write("0\t" + str(DN) + "\t" + Zenith)

The Zenith string already contains the trailing \n from the original file when you read it in.

Upvotes: 8

Related Questions