JamalCrawford..
JamalCrawford..

Reputation: 187

Append to data file to file using python

I need help taking data from file a to file b and appending to both sides of it. so file a has a list of numbers and I want to put it on a new file but appending "<jno> 1st piece of data from file a <\jno>\n"

Here is my code as of now....

def code(filename):

    file=open(filename)

    FinishFile=open("JimmenyCricketsXML.txt","w")

    FinishFile.write('<team>\n')
    FinishFile.write('<crickets>\n')


    for element in file:
        FinishFile.write('<jno>'+ str(element) + '<\jno>\n')

my results are:

<jno>element

<\jno>  

why does it skip to the next line?

Upvotes: 0

Views: 104

Answers (2)

Lumi.seven
Lumi.seven

Reputation: 13

Try use:

FinishFile.write('<jno>'+ str(element).strip('\n') + '</jno>\n')

Or

FinishFile.write('<jno>'+ str(element).strip('\n') + '<\\jno>\n')

Upvotes: 1

Byte Commander
Byte Commander

Reputation: 6736

Replace the line

   FinishFile.write('<jno>'+ str(element) + '<\jno>\n')

with:

   FinishFile.write('<jno>'+ str(element).strip() + '<\jno>\n')

To remove leading and trailing whitespace characters (including linebreaks).

If you only want to remove linebreaks and no other spaces etc., use this line instead:

   FinishFile.write('<jno>'+ str(element).strip('\n') + '<\jno>\n')

Upvotes: 1

Related Questions