2

i am trying to prepend each line of a file with " (2 spaces/tabs after ") and append with string- "\r\n"+". the lines of the file before this operation looks like as folllows.

        <!--You have a CHOICE of the next 5 items at this level-->
        <!--Optional:-->
        <urn:Account id=\"?\">
           <!--Optional:-->
           ............
           .............    

I am using the following code,

inf=open("req.txt","r")

outf=open("out.txt","w")

for line in inf.readlines():

    outf.write("\"          "+line+"\\r\\n\" +")

inf.close()

outf.close()

prepending is happening as expected but appending is not happening properly. Final result was all lines were prepended with - \r\n" +" except the first line. first line was prepended with only " .

I want each line prepended with " and appended with "\r\n"+"

2
  • 1
    Just a comment: readlines() is quite inefficient for large files -- just iterate over the file object itself instead. Commented May 29, 2012 at 7:32
  • how to do that, please give sample code. Commented May 29, 2012 at 7:57

2 Answers 2

1

You should probably use python's built in string formatting.

outf.write("%s%s%s" % ('"          ', line, '\r\n" +'))

However! You're not removing the newlines from your data before changing it. As a result you're getting your format, the entire line (including the newline) and then your second part.

You'll want to run it through python's built in rstrip method.

outf.write("%s%s%s" % ('"          ', line.rstrip(), '\r\n" +'))

One thing you want to watch out for is that rstrip will remove any white space, if you want to remove just newlines then you can use:

outf.write("%s%s%s" % ('"          ', line.rstrip("\r\n"), '\r\n" +'))

However, once you do this, you'll need to put a new line at the end of your string again.

outf.write("%s%s%s%s" % ('"          ', line.rstrip("\r\n"), '\r\n" +', "\r\n"))

However! Depending on your OS your default line ending may be different, to do it correctly you'll want to import os then:

outf.write("%s%s%s%s" % ('"          ', line.rstrip(os.linesep), '\r\n" +', os.linesep))
Sign up to request clarification or add additional context in comments.

4 Comments

Just out of curiosity, why aren't all \r\n replaced with os.linesep?
You can indeed do that for the parameter to rstrip but the 3rd string argument is a literal \r\n not a carriage return and a line feed. In the case of rstrip, it doesn't really matter, because the line ending is either going to be \r or \n or \r\n depending on your OS, but I changed it to be consistent. :)
The information provided by OmnipotentEntity is useful for me but the code given did not work for me. but the below code worked for me.
l = '"%s\\r\\n"+\n' %(line[:-1]) outf.write(l)
1

Is there a reason you are not using a xml module to read/write the xml file? Because it could simplify your code.

Edit: Here's a PyMOTW ElementTree tutorial, and the Docs python.org/xml.etree.ElementTree

1 Comment

really i do not know about that module

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.