2

So the problem I have is that I need to be able to take an existing text file and make a new file adding xml tags to each line. For example:

Hello
World
This
Is
The
Text

should be written to a new text file like this

<tag1>Hello</tag1>
<tag2>World</tag2>
<tag3>This</tag3>
<tag4>Is</tag4>
<tag5>The</tag5>
<tag6>Text</tag6>

I know how to open and write to a new file but having trouble manipulating the data, I'm still a noob and this is my first question on stackoverflow so any help would be greatly appreciated.

3 Answers 3

1

This is a very simple problem that you should be able to solve on your own. Since you're having some trouble, I'll show you how to, generally, attack most programming problems.

First, divide the problem. You have a text file which you must modify to include xml tags. You've correctly identified that opening and writing to files are separate problems - and you already know how to solve those. Futhermore, it's obvious but worth mentioning that you're dealing with a similar repeating pattern on each line. So, if you know how to transform each line of the text into what you want, you'll have solved the problem. Now, let's look at that first line:

Hello

becomes

<tag1>Hello</tag1>

What steps must be performed in order to transform one into the other? Well, the second is just the first with some text inserted at the beggining and end of the string! This is called string concatenation, and simply googling will find you an answer immediately. Further, you have the issue of the tag number, which is different on each line. This is solved by keeping a counter variable, converting the number to a string, and concatenating it too.

Here's an example of a counter:

for i in range(10):
    print i

and another:

i=0
while i<10:
    print i
    i+=1

That's it. I'm confident that you can solve the problem on your own now.

Sign up to request clarification or add additional context in comments.

Comments

1

If you want way to do simply, open the text file with vim and type following.

:%s!\(.*\)!\=printf("<tag%d>%s</tag%d>", line("."), submatch(1), line("."))!g

Or if you want to use python,

from xml.sax.saxutils import escape
with open('file.txt', 'r') as f:
  print "\n".join((lambda y: ["<tag%d>%s</tag%d>" % (i+1, escape(y[i].strip()), i+1) for i in range(0, len(y))])(f.readlines()))

Also an answer from @goncalopp

from xml.sax.saxutils import escape
for i,line in enumerate(open('file.txt')):
  print "<tag{0}>{1}</tag{0}>".format(i+1,escape(line.strip()))

3 Comments

That looks over complicated. There's no need to import and learn a whole library for something this simple
@goncalopp used for escape().
Yes, I noticed. Nothing the OP has posted suggests the need for escaping, IMHO. If he needs escaping later, he'll learn about it, instead of avoiding the problem. It's not just the library, though; compare the readability of your solution to this: for i,line in enumerate(open('file.txt')): print "<tag{0}>{1}</tag{0}>".format(i,line) . Lambdas and list comprehensions have their place, but won't help a self-entitled noob
-1
>>> f = open('file.txt', 'r')
>>> lines = f.readlines()
>>> for index in range(len(lines)):
...     print '<tag%s>%s</tag%s>'%(index+1, lines[index].strip(), index+1)
... 
<tag1>Hello</tag1>
<tag2>World</tag2>
<tag3>This</tag3>
<tag4>Is</tag4>
<tag5>The</tag5>
<tag6>Text</tag6>
Hope you know how to write into file.

Comments

Your Answer

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.