Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

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

like image 476
robintw Avatar asked Aug 08 '26 12:08

robintw


1 Answers

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.

like image 64
John Machin Avatar answered Aug 11 '26 00:08

John Machin