[Python-ideas] Subprocess: Add an encoding argument

Paul Moore p.f.moore at gmail.com
Mon Sep 1 21:29:46 CEST 2014


On 1 September 2014 20:14, Akira Li <4kir4.1i at gmail.com> wrote:
> Could you provide examples how the final result could look like?

Do you mean what I'm proposing?

p = Popen(..., encoding='utf-8')
p.stdout is now a text stream assuming the data is in UTF8, rather
than assuming it's in the default encoding.

> For example, to read utf-8 encoded byte stream as a text with universal
> newline mode enabled:
>
>   with (Popen(cmd, stdout=PIPE, bufsize=1) as p,
>         TextIOWrapper(p.stdout, encoding='utf-8') as pipe):
>       for line in pipe:
>           process(line)

That looks like sort of what I had in mind as a workaround. I hadn't
tried it to confirm it worked, though.

> Or the same, all at once:
>
>    lines = check_output(cmd).decode('utf-8').splitlines() #XXX issue22232

Yes, essentially, although the need for an explicit decode feels a bit
ugly to me...

Paul


More information about the Python-ideas mailing list