2

I am parsing xml file using:

  lxml.etree.parse(xmlFile)  

I extracted some node attributes which contain single backslash and save them into dictionary

Then I write the dictionary into a file using:

f = open(myFile, 'w')
for k, v in sorted(dic.items()):
    f.write(str((k,v)))
    f.write('\n')
f.flush()
f.close()

Know the problem is that after parsing if I write the tree into a file using:

     tree.write('output4.xml')

the tree is exactly as the original file BUT the dictionary that saved into myFile has \\ instead of each \ So why python adding \ wherever it found one.

Example: this is the original attribute:

"\displaystyle\mathbb{Z}_{n}\longrightarrow\mathbb{Z}"

and in the dictionary file it becomes:

'\\\displaystyle\\\mathbb{Z}_{n}\\\longrightarrow\\\mathbb{Z}'
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
MathGirl
  • 75
  • 10

1 Answers1

0

str((k,v)) uses the repr of k and v:

>>> k = r'\textit{foo}'
>>> v = 'bla'
>>> s = str((k, v))
>>> s
"('\\\\textit{foo}', 'bla')"

This has the benefit of being deserializable with ast.literal_eval, because the escaped strings from repr have the same syntax as Python literals:

>>> import ast
>>> print(ast.literal_eval(s)[0])

If this is not what you want, you'll have to find some other way of writing out the pairs.

Community
  • 1
  • 1
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • ok this is good in a way that it write the key correctly but it does not write the value. I tried to make it as tuple but this gives error that it only can write str – MathGirl Oct 15 '14 at 11:44