problems getting rid of extra newlines

This is the place for queries that don't fit in any of the other categories.

problems getting rid of extra newlines

Postby kgd » Tue Oct 15, 2013 12:13 am

Hi, I'm using Python 2.7.3 and am working on a script to read in a matrix file line by line, then parse and write lines to a new file. My script seems to work fine, except that it inserts an extra newline after processing each line in the file. I've tried several different ways to write '\n' to the outfile, and I've tried both 'w' and 'wb' modes, but so far I get either no newline or one too many. It's probably something incredibly simple, but I can't see it. Can you help? Here's what I've written so far:

Code: Select all
import sys
def main():
   #list required arguments and open files    
   if len(sys.argv)!=4:
      print "Enter program_name, infile, outfile, reagent"
      sys.exit()      
   infile = open(sys.argv[1]).readlines()
   outfile = open(sys.argv[2], 'w')
   #save infile header line to hline1 and split into parts
   hline1 = infile.pop(0)
   hparts = hline1.split('\t')
   reagent = sys.argv[3]
   #write outfile header line
   outfile.write('value' + '\t' + 'reagent' + '\t' + 'patient' + '\t' + 'antigen' + '\n')
   # read matrix rows and split into parts
   for line in infile:
      line = line.rstrip()
      parts = line.split('\t')
      patient = parts[0]
      #Use loop to print parts to outfile
      for i in range(1,len(parts)):   
         outfile.write(parts[i] + '\t' + reagent + '\t' + patient + '\t' + hparts[i] + '\n')
   #print reference line   
   print "The file been written to", outfile      
main()


I've attached a sample test1.txt file and a testout.txt file (without the extra newlines to show what I want).

Thanks in advance!
Attachments
testout.txt
Sample desired outfile
(356 Bytes) Downloaded 7 times
test1.txt
sample infile
(127 Bytes) Downloaded 8 times
Last edited by micseydel on Tue Oct 15, 2013 12:14 am, edited 1 time in total.
Reason: Code tags, locked OP.
kgd
 
Posts: 3
Joined: Mon Oct 14, 2013 11:46 pm

Re: problems getting rid of extra newlines

Postby Crimson King » Tue Oct 15, 2013 5:05 am

Hey kgd welcome to the forum, be sure to read this thread viewtopic.php?f=10&t=145

Concerning your problem, you get a new line everytime you write the last item of your list called 'hparts'. That happens because hparts has these elements:

Code: Select all
hparts >> ['P', 'A', 'B', 'C', 'D', 'E\n']


That can be avoided by simply changing this line:
Code: Select all
hparts = hline1.split('\t')
to
Code: Select all
hparts = hline1.rstrip().split('\t')


Cheers!
Last edited by micseydel on Tue Oct 15, 2013 8:02 am, edited 1 time in total.
Reason: Removed quote since OP is locked.
Ubuntu 13.04, Archlinux and Windows 7.

Python 2.7.3
User avatar
Crimson King
 
Posts: 57
Joined: Fri Mar 08, 2013 2:42 pm
Location: Buenos Aires, Argentina

Re: problems getting rid of extra newlines

Postby kgd » Tue Oct 15, 2013 1:22 pm

Never mind. I figured it out. I had forgotten that I didn't perform an rstrip on the header line. Fixed the problem by modifying line 30:

Code: Select all
         outfile.write(parts[i] + '\t' + reagent + '\t' + patient + '\t' + str.rstrip(hparts[i]) + '\n')


Thanks for letting me talk myself through this online!

kgd
kgd
 
Posts: 3
Joined: Mon Oct 14, 2013 11:46 pm

Re: problems getting rid of extra newlines

Postby kgd » Tue Oct 15, 2013 1:26 pm

Thanks, micseydel! Your solution is cleaner than mine.
kgd
 
Posts: 3
Joined: Mon Oct 14, 2013 11:46 pm

Re: problems getting rid of extra newlines

Postby micseydel » Tue Oct 15, 2013 5:22 pm

kgd wrote:Thanks, micseydel! Your solution is cleaner than mine.

It was Crimson King who helped you, not me. I edited his post to remove the quote after locking your original post, to maintain a better "signal to noise ratio".
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 939
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: cakefiend, metulburr, Yoriz and 1 guest