Skip to main content
added 48 characters in body; edited tags
Source Link
OmnipotentEntity
  • 17.2k
  • 6
  • 69
  • 103

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. <urn:WorkDocuments messageType="FLAT">

        <!--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():

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

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

for line in inf.readlines():

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

inf.close()

outf.close()

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"+"

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. <urn:WorkDocuments messageType="FLAT">

        <!--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"+"

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. <urn:WorkDocuments messageType="FLAT">

        <!--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"+"

Source Link
user1423015
  • 663
  • 1
  • 6
  • 12

string append in python

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. <urn:WorkDocuments messageType="FLAT">

        <!--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"+"