tags:

views:

83

answers:

1

Hi, I'm using an xml file to store configurations for a software.

One of theese configurations would be a system path like

> set_value = "c:\\test\\3 tests\\test"

i can store it by using:

> setting = etree.SubElement(settings,
> "setting", name=tmp_set_name, type =
> set_type , value= set_value)

If I use

doc.write(output_file, method='xml',encoding = 'utf-8', compression=0)

the file would be:

< setting type="str" name="MyPath" value="c:\test\3 tests\test"/>

Now I read it again with the etree.parse method

I obtain an etree child object with a string value, but the string contains the

\3

character and if i try to use it to write again to xml it will be interpreted !!!!! So i cannot use it anymore as a path Maybe i'm only missing a simple string operation, but I cannot see it =) How would you solve it in a smart way ?

This is an example, but what is the best way, you think to store paths in xml and parse them with lxml ?

Thank you !!

+1  A: 

Now I read it again with the etree.parse method

I obtain an etree child object with a string value, but the string contains the

\3

character and if i try to use it to write again to xml it will be interpreted !!!!!

I just tried that, and it doesn't get "interpreted". The elements attributes as returned after parsed is:

{'type': 'str', 'name': 'yowza!', 'value': 'c:\\test\\3 tests\\test'}

So as you see this works just as you expected it to work. If you really have this problem, you are doing something else than what you are saying. Show us the real code, or make a small example code where you demonstrate the problem and use that.

Lennart Regebro
Thank you, your test is very useful.I use the values I retireve from xml to create variables inside a setting object. The problem was in this passage as the code did:exec('self.%s = %s' %(k,v))where k was the attribute name and v the valuethe \\ in value when substituted in %s where interpreted.When i read from the object to create xml i read the string without double \\I erroneously thought the problem was in the lxml print but it was the passage before.
nios
Why in heavens name do you use an exec? setattr(self, k, v)
Lennart Regebro
+1 Dear Lord, don't ever use exec!
bobince
It wasn't my code, i inherited it, i've removed it now =)
nios