logo
down
shadow

Encoding in XML declaration python


Encoding in XML declaration python

By : user3863081
Date : January 02 2021, 06:48 AM
it helps some times I have created an XML file using python. But the XML declaration has only version info. How can I include encoding with XML declaration like:
code :


Share : facebook icon twitter icon
Should I use encoding declaration in Python 3?

Should I use encoding declaration in Python 3?


By : D. Summerville
Date : March 29 2020, 07:55 AM
may help you . Because the default is UTF-8, you only need to use that declaration when you deviate from the default, or if you rely on other tools (like your IDE or text editor) to make use of that information.
In other words, as far as Python is concerned, only when you want to use an encoding that differs do you have to use that declaration.
How to parse an XML file with encoding declaration in Python?

How to parse an XML file with encoding declaration in Python?


By : Anna Sanina
Date : March 29 2020, 07:55 AM
wish help you to fix your issue One thing that I tried, that worked for me is to open the xml file as a file object , then use ElementTree.fromstring() passing in the complete contents of the file.
Example -
code :
>>> import xml.etree.ElementTree as ET
>>> ef = ET.parse('a.xml')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python34\lib\xml\etree\ElementTree.py", line 1187, in parse
    tree.parse(source, parser)
  File "C:\Python34\lib\xml\etree\ElementTree.py", line 598, in parse
    self._root = parser._parse_whole(source)
ValueError: multi-byte encodings are not supported
>>> with open('a.xml','r') as f:
...     ef = ET.fromstring(f.read())
...
>>> ef
<Element 'productMeta' at 0x028DF180>
import xml.etree.ElementTree as ET
xmlp = ET.XMLParser(encoding="utf-8")
f = ET.parse('a.xml',parser=xmlp)
python: Dowloading and caching XML files - how to handle encoding declaration?

python: Dowloading and caching XML files - how to handle encoding declaration?


By : Ajinkya Gurav
Date : March 29 2020, 07:55 AM
around this issue , The file needs to be opened in binary mode rather than text mode.
code :
open(my_file, 'rb') # b stands for binary
Python encoding declaration appears to be getting ignored?

Python encoding declaration appears to be getting ignored?


By : user7479034
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , When we declare # -*- coding: ... -*- (or just # coding: ...), we're telling Python the source file is saved in the declared encoding.
So, encoding declaration matters, but until the source file is valid in the declared encoding and we don't use u-literals, changing the declared encoding will not affect the printout.
code :
# -*- coding: utf-8 -*-
print repr('Á') # '\xc3\x81'
# -*- coding: cp1252 -*-
print repr('Á')
SyntaxError: 'charmap' codec can't decode byte 0x81 in position 13: character maps to <undefined>
# -*- coding: utf-8 -*-
print repr('é') # '\xc3\xa9'
# -*- coding: cp1252 -*-
print repr('é') # '\xc3\xa9'
import sys
bytes_in = '\xc3\xa9'
enc_out = sys.stdout.encoding
chars_out = '\xc3\xa9'.decode(enc_out)
print chars_out # 'é' if enc_out == 'utf-8'
# -*- coding: utf-8 -*-
print repr(u'é') # u'\xe9'
# -*- coding: cp1252 -*-
print repr(u'é') # u'\xc3\xa9'
print repr(unicode('\xc3\xa9', 'cp1252')) # u'\xc3\xa9'
print unicode('\xc3\xa9', 'cp1252') # é
# -*- coding: cp1252 -*-
print u'é' # é
Write .xml in Python with pretty print and encoding declaration

Write .xml in Python with pretty print and encoding declaration


By : Bhupendra Prajapati
Date : March 29 2020, 07:55 AM
this will help The most elegant solution is certainly using the third-party library lxml, which is being used a lot – for good reasons. It offers both a pretty_print and an xml_declaration parameter in the tostring() method, so you get both. And the API is quite close to that of the std-lib ElementTree, which you seem to be using now. Here's an example:
code :
>>> from lxml import etree
>>> doc = etree.parse(xmlPath)
>>> print etree.tostring(doc, encoding='UTF-8', xml_declaration=True,
                         pretty_print=True)
<?xml version='1.0' encoding='UTF-8'?>
<main>
  <sub>
    <name>Ana</name>
    <detail/>
    <type>smart</type>
  </sub>
</main>
>>> doc = minidom.parseString(ET.tostring(root))
>>> print doc.toprettyxml(encoding='utf8')
<?xml version="1.0" encoding="utf8"?>
<main>
    <sub>
        <name>Ana</name>
        <detail/>
        <type>smart</type>
    </sub>
</main>
Related Posts Related Posts :
  • google app engine error ,and i can't open it now.(python)
  • IIS Not Linking to Django with PyISAPIe
  • Create two separate windows in terminal
  • Killing a subprocess including its children from python
  • google search rankings and trends api
  • Easiest way to automatically download required modules in Python?
  • How to read a string one letter at a time in python
  • Python pixel manipulation library
  • python cx_Freeze egg problem
  • How can I select all rows with sqlalchemy?
  • Converting IPv4 or IPv6 address to a long for comparisons
  • Converting BMP to Grey Scale in Image Module Python
  • Python: Memory usage and optimization when modifying lists
  • Mod_wsgi versus fapws3 - Django
  • In Django, how to create tables from an SQL file when syncdb is run
  • What's the pythonic way to use getters and setters?
  • python regular expression for domain names
  • Pickling a class definition
  • How to catch login failures with PySVN?
  • Python: how to enclose strings in a list with < and >
  • Python: finding lowest integer
  • efficiently finding the interval with non-zeros in scipy/numpy in Python?
  • How to convert Selenese (html) to Python programmatically?
  • fade out in Image module Python
  • local variable 'sresult' referenced before assignment
  • how to transfer a python object between two requests?
  • Getting weird python error when I run a simple django script in Eclipse, not happening in console
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk