This issue tracker has been migrated to GitHub ,
and is currently read-only.
For more information,
see the GitHub FAQs in the Python's Developer Guide.
Created on 2012年04月01日 15:28 by tshepang, last changed 2022年04月11日 14:57 by admin. This issue is now closed.
| Files | ||||
|---|---|---|---|---|
| File name | Uploaded | Description | Edit | |
| issue14465.patch | alex.henderson, 2013年08月05日 21:14 | pretty printer patch, as implemented for issue 17372. | review | |
| Pull Requests | |||
|---|---|---|---|
| URL | Status | Linked | Edit |
| PR 4016 | closed | alex.dzyoba, 2017年10月17日 07:47 | |
| PR 8933 | closed | mcepl, 2018年08月25日 19:22 | |
| PR 15200 | merged | scoder, 2019年08月10日 17:06 | |
| Messages (16) | |||
|---|---|---|---|
| msg157299 - (view) | Author: Tshepang Lekhonkhobe (tshepang) * | Date: 2012年04月01日 15:28 | |
I often miss lxml's "pretty_print=True" functionality. Can you implement something similar. |
|||
| msg157317 - (view) | Author: Martin v. Löwis (loewis) * (Python committer) | Date: 2012年04月01日 16:49 | |
Would you like to provide a patch? |
|||
| msg157320 - (view) | Author: Eli Bendersky (eli.bendersky) * (Python committer) | Date: 2012年04月01日 17:59 | |
Tshepang, Frankly, there are a lot of issues to solve in ElementTree (it hasn't been given love in a long time...) and such features would be low priority, as I'm not getting much help and am swamped already. As Martin said, patches can go a long way here... |
|||
| msg157325 - (view) | Author: Tshepang Lekhonkhobe (tshepang) * | Date: 2012年04月01日 19:08 | |
Okay, I will try, even though C scares me. |
|||
| msg157647 - (view) | Author: Éric Araujo (eric.araujo) * (Python committer) | Date: 2012年04月06日 06:24 | |
You may be able to code it entirely in the Python part of the module (adding a new parameter to Element.write and tostring). |
|||
| msg194313 - (view) | Author: Eli Bendersky (eli.bendersky) * (Python committer) | Date: 2013年08月03日 22:26 | |
A patch exists in the duplicate #17372 |
|||
| msg194508 - (view) | Author: Alex Henderson (alex.henderson) * | Date: 2013年08月05日 21:14 | |
Proposed patch copied over from duplicate issue 17372. |
|||
| msg194902 - (view) | Author: Stefan Behnel (scoder) * (Python committer) | Date: 2013年08月11日 16:30 | |
Just to reiterate this point, lxml.etree supports a "pretty_print" flag in its tostring() function and ElementTree.write(). It would thus make sense to support the same thing in ET. http://lxml.de/api.html#serialisation For completeness, the current signature looks like this: def tostring(element_or_tree, *, encoding=None, method="xml", xml_declaration=None, pretty_print=False, with_tail=True, standalone=None, doctype=None, exclusive=False, with_comments=True, inclusive_ns_prefixes=None): (The last three options are for C14N serialisation.) |
|||
| msg304362 - (view) | Author: STINNER Victor (vstinner) * (Python committer) | Date: 2017年10月13日 21:16 | |
For the record, at 2015年04月02日, the bpo-23847 has been marked as a duplicate of this issue. |
|||
| msg304872 - (view) | Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) | Date: 2017年10月24日 08:46 | |
My thoughts: 1. Whitespaces are significant in XML. Pretty-printed XML is different from the original XML to an XML parser. For some applications some whitespaces around tags are not significant. But this depends on the application and in different parts of the document whitespaces can have different meaning. For example the document can contain a metadata with insignificant whitespaces and marked up text with significant whitespaces. There is a special attribute named xml:space that can signal the meaning of whitespaces for the part of a document. https://www.w3.org/TR/xml/#sec-white-space 2. In HTML whitespaces around <P> are insignificant, but whitespaces around <I> are significant. Whitespaces inside <PRE>...</PRE> are significant. 3. If strip whitespaces around tags and insert newlines and indentations, shouldn't we strip whitespaces inside the text context? Or preserve newlines but update indentations? 4. If modify whitespaces on output, it may be worth to add an option to ignore insignificant whitespaces on input. 5. Serialization of ElementTree in the stdlib is much slower than in lxml (see issue25881). Perhaps it should be implemented in C. But it should be kept simple for this. Pretty-printing can be implemented as an outher preprocessing operation (for example the original Eli's code indents the tree in-place: http://effbot.org/zone/element-lib.htm#prettyprint) or as a proxy that indents elements on-fly. |
|||
| msg323690 - (view) | Author: Stefan Behnel (scoder) * (Python committer) | Date: 2018年08月18日 05:06 | |
> Serialization of ElementTree in the stdlib is much slower than in lxml (see issue25881). Perhaps it should be implemented in C. But it should be kept simple for this. Should I say it? That's a first class use case for Cython. > Pretty-printing can be implemented as an outher preprocessing operation Agreed. And that would actually be much simpler to implement in C. |
|||
| msg324098 - (view) | Author: Raymond Hettinger (rhettinger) * (Python committer) | Date: 2018年08月25日 20:00 | |
A few more thoughts for consideration: * We already have a toprettyxml() tool in the minidom package. * Since whitespace is significant in XML, prettifying changes the content and meaning, so it doesn't round-trip and should only be used for debugging purposes. * Usually, I recommend using XML viewers such as the one built into the Chrome browser. That provides indentation without changing meaning. It also lets you run searches and conveniently supports folding and unfolding elements. I would rather someone use a viewer rather than something like toprettyxml(). |
|||
| msg335306 - (view) | Author: Clayton Olney (Clayton Olney) | Date: 2019年02月12日 14:36 | |
I have a use case where the receiving application is expecting the indentation, and I need to run my code in Lambda. So, lxml is out of the question. |
|||
| msg349326 - (view) | Author: Raymond Hettinger (rhettinger) * (Python committer) | Date: 2019年08月10日 03:08 | |
FWIW, here is the relevant section of the XML specification, https://www.w3.org/TR/2008/REC-xml-20081126/#sec-white-space : """In editing XML documents, it is often convenient to use "white space" (spaces, tabs, and blank lines) to set apart the markup for greater readability. Such white space is typically not intended for inclusion in the delivered version of the document. On the other hand, "significant" white space that should be preserved in the delivered version is common, for example in poetry and source code. An XML processor must always pass all characters in a document that are not markup through to the application. A validating XML processor must also inform the application which of these characters constitute white space appearing in element content. """ OTOH, the java TransformerFactory does support a property, OutputKeys.INDENT, so there is a precedent for this feature request. Stefan, would you please make a final determination or pronouncement on whether this makes sense for ElementTree or whether it is outside the scope of what the module is trying to accomplish. |
|||
| msg349346 - (view) | Author: Stefan Behnel (scoder) * (Python committer) | Date: 2019年08月10日 17:06 | |
The spec section that Raymond quoted makes it clear that pretty printing is not for everyone. But there are many use cases where it is 1) helpful, 2) leads to correct results, and 3) does not grow the file size excessively. Whoever wants to make use of it is probably in such a situation. I think adding some kind of support in the standard library would be nice, but it should not hurt "normal" uses, especially when a lot of data is involved. I'll send a PR that adds an indent() function to pre-process trees. Comments welcome. |
|||
| msg350301 - (view) | Author: Stefan Behnel (scoder) * (Python committer) | Date: 2019年08月23日 14:44 | |
New changeset b5d3ceea48c181b3e2c6c67424317afed606bd39 by Stefan Behnel in branch 'master': bpo-14465: Add an indent() function to xml.etree.ElementTree to pretty-print XML trees (GH-15200) https://github.com/python/cpython/commit/b5d3ceea48c181b3e2c6c67424317afed606bd39 |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022年04月11日 14:57:28 | admin | set | github: 58670 |
| 2019年08月23日 14:45:28 | scoder | set | status: open -> closed resolution: fixed stage: patch review -> resolved |
| 2019年08月23日 14:44:37 | scoder | set | messages: + msg350301 |
| 2019年08月12日 13:07:08 | vstinner | set | nosy:
- vstinner |
| 2019年08月10日 17:06:37 | scoder | set | pull_requests: + pull_request14929 |
| 2019年08月10日 17:06:16 | scoder | set | assignee: serhiy.storchaka -> scoder messages: + msg349346 versions: + Python 3.9, - Python 3.7, Python 3.8 |
| 2019年08月10日 03:08:27 | rhettinger | set | messages: + msg349326 |
| 2019年08月08日 15:53:22 | Andrew Grant | set | nosy:
+ Andrew Grant |
| 2019年06月22日 20:31:20 | mitar | set | nosy:
+ mitar |
| 2019年02月12日 14:36:01 | Clayton Olney | set | nosy:
+ Clayton Olney messages: + msg335306 |
| 2018年08月25日 20:00:03 | rhettinger | set | nosy:
+ rhettinger messages: + msg324098 |
| 2018年08月25日 19:22:26 | mcepl | set | pull_requests: + pull_request8405 |
| 2018年08月18日 05:06:24 | scoder | set | messages: + msg323690 |
| 2018年08月17日 15:23:10 | mcepl | set | versions: + Python 3.8 |
| 2017年10月24日 08:46:06 | serhiy.storchaka | set | messages: + msg304872 |
| 2017年10月22日 20:32:54 | wolma | set | nosy:
+ wolma |
| 2017年10月18日 12:19:04 | serhiy.storchaka | set | assignee: serhiy.storchaka nosy: + serhiy.storchaka components: + XML versions: + Python 3.7, - Python 3.4 |
| 2017年10月17日 07:47:12 | alex.dzyoba | set | stage: patch review pull_requests: + pull_request3990 |
| 2017年10月14日 17:31:29 | alex.dzyoba | set | nosy:
+ alex.dzyoba |
| 2017年10月13日 21:16:29 | vstinner | set | nosy:
+ vstinner messages: + msg304362 |
| 2015年04月02日 08:20:47 | serhiy.storchaka | link | issue23847 superseder |
| 2015年01月27日 10:23:02 | martin.panter | set | nosy:
+ martin.panter |
| 2015年01月27日 09:26:05 | mcepl | set | nosy:
+ mcepl |
| 2013年08月11日 17:56:38 | eric.snow | set | nosy:
+ eric.snow |
| 2013年08月11日 16:30:21 | scoder | set | nosy:
+ scoder messages: + msg194902 versions: + Python 3.4, - Python 3.3 |
| 2013年08月05日 21:14:48 | alex.henderson | set | files:
+ issue14465.patch nosy: + alex.henderson messages: + msg194508 keywords: + patch |
| 2013年08月03日 22:26:04 | eli.bendersky | set | messages: + msg194313 |
| 2013年08月03日 22:25:43 | eli.bendersky | link | issue17372 superseder |
| 2012年04月06日 06:24:10 | eric.araujo | set | nosy:
+ eric.araujo messages: + msg157647 |
| 2012年04月05日 22:20:48 | santoso.wijaya | set | nosy:
+ santoso.wijaya |
| 2012年04月01日 19:08:10 | tshepang | set | messages: + msg157325 |
| 2012年04月01日 17:59:20 | eli.bendersky | set | messages: + msg157320 |
| 2012年04月01日 16:49:32 | loewis | set | nosy:
+ loewis messages: + msg157317 |
| 2012年04月01日 16:43:46 | r.david.murray | set | type: enhancement |
| 2012年04月01日 15:29:13 | tshepang | set | title: add feature to prettify XML output -> xml.etree.ElementTree: add feature to prettify XML output |
| 2012年04月01日 15:28:03 | tshepang | create | |