Re: [Python-Dev] RFC: PEP 460: Add bytes % args and bytes.format(args) to Python 3.5

2014年1月11日 12:42:34 -0800

On 2014年1月11日 11:54:26 -0800, Ethan Furman <[email protected]> wrote:
> On 01/11/2014 11:49 AM, Stephen J. Turnbull wrote:
> > MRAB writes:
> >
> > > > with open("outfile.pdf", "w", encoding="latin-1") as f:
> > > > f.write(pdf)
> > > >
> > > [snip]
> > > The second example won't work because you're forgetting about the
> > > handling of line endings in text mode.
> >
> > Not so fast! Forgot, yes (me too!), but not work? Not quite:
> >
> > with open("outfile.pdf", "w", encoding="latin-1", newline="") as f:
> > f.write(pdf)
> >
> > should do the trick.
> 
> Well, it's good that there is a work-a-round. Are we going to have a 
> document listing all the work-a-rounds needed to 
> program a bytes-oriented style using unicode?
That's not a work-around (if you are talking specifically about the
newline=""). That's just the way the python3 IO library works. If you
want to preserve the newlines in your data, but still have the text-io
machinery count them for deciding when to trigger io/buffering behavior,
you use newline=''.
It's not the most intuitive API, so I won't be surprised if a lot of
people don't know about it or get confused by it when they see it.
I first learned about it in the context of csv files, another one of
those legacy file protocols that are mostly-text-but-not-entirely.
--David
_______________________________________________
Python-Dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to