can not specify newline of a file

Bug #1791945 reported by Zhan Shiyan on 2018-09-11
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
lxml
Undecided
Unassigned

Bug Description

For some reason I need to dump XML files with DOC newline characters.
But I didn't find a way to specify the newline character in lxml library.

I searched the source code and found lxml wrote the file in binary mode and used '\n' as newline character.

So I wonder if there would be some way (or in the future) to specify the newline character.

Python : sys.version_info(major=3, minor=6, micro=2, releaselevel='final', serial=0)
lxml.etree : (4, 2, 1, 0)
libxml used : (2, 9, 5)
libxml compiled : (2, 9, 5)
libxslt used : (1, 1, 30)
libxslt compiled : (1, 1, 30)

scoder (scoder) wrote :

It's not clear what you tried, but if you want pretty printing, for example, then you can just add your own line endings to the document. See http://effbot.org/zone/element-lib.htm#prettyprint

Zhan Shiyan (zsynew) wrote :

I tried following code using lxml:

Note: `ele_root` is generated by some other code.

1.
tree = etree.ElementTree(ele_root)
with open(all_in_one_file_name, 'wb') as output_file:
    tree.write(output_file, pretty_print=True, xml_declaration=True, encoding='utf-8',
               doctype='<!DOCTYPE reference PUBLIC "-//OASIS//DTD DITA Reference//EN" "reference.dtd">')

2.
tree = etree.ElementTree(ele_root)
tree.write(all_in_one_file_name, pretty_print=True, xml_declaration=True, encoding='utf-8',
                      doctype=doctype)

scoder (scoder) wrote :

Yes, pretty printing adds '\n' newlines. That's how it is. As I said, use your own indenting for it, that's simple enough.

Changed in lxml:
status: New → Won't Fix
scoder (scoder) wrote :

Closing as "invalid", since serialisation (and thus pretty printing) is done in libxml2 and not lxml.

Changed in lxml:
status: Won't Fix → Invalid
To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers