0

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?

Mr Lister
46.8k15 gold badges118 silver badges156 bronze badges
asked Dec 2, 2015 at 15:05
4
  • So, are you getting an error message, or what? Commented Dec 2, 2015 at 15:07
  • yeah it splits it line 1 saying '<jno>element, line 2 <\jno> Commented Dec 2, 2015 at 15:10
  • Does your element string contain new lines? Commented Dec 2, 2015 at 15:12
  • Does writing \j actually output "\j", is that guaranteed? Commented Dec 7, 2015 at 15:55

2 Answers 2

1

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')
answered Dec 2, 2015 at 15:16
Sign up to request clarification or add additional context in comments.

Comments

1

Try use:

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

Or

FinishFile.write('<jno>'+ str(element).strip('\n') + '<\\jno>\n')
answered Dec 2, 2015 at 15:42

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.