Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #197876

Re: open: 'ascii', 'backslashreplace' not behaving as expected - why?

From Greg Ewing <greg.ewing@canterbury.ac.nz>
Newsgroups comp.lang.python
Subject Re: open: 'ascii', 'backslashreplace' not behaving as expected - why?
Date 2026-08-10 12:08 +1200
Message-ID <ndsj32Fq77oU1@mid.individual.net> (permalink)
References <1155tpv$13on7$1@dont-email.me> <1156074$14bmn$1@dont-email.me> <1156av3$15v7o$1@dont-email.me> <1156b47$15v7o$2@dont-email.me> <1156bib$15v7o$3@dont-email.me>

Show all headers | View raw


On 8/08/26 4:33 pm, Veek M wrote:
> it's the raw utf-8 encoded as bytes but since it is a unicode string why
> doesn't he save it as u'\xef\xbf\xbf' why does he escape the '\' and make
> it '\\x'

Because you decoded it as ascii with backslashreplace. It's replacing 
each byte that's outside the ascii range with four characters: a 
backslash, an 'x', and two hex digits. The backslashes are being doubled 
when you print the string and its repr() gets computed.

Since the file is actually utf-8 and not ascii, that's the appropriate 
way to decode it:

   fh = open('/tmp/x', 'rt', encoding = 'utf-8')

Then your ffff should come through as a single character in the string 
and print as '\uffff'.

-- 
Greg



Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

open: 'ascii', 'backslashreplace' not behaving as expected - why? Veek M <veekjunk@foobar.com> - 2026-08-08 00:38 +0000
  Re: open: 'ascii', 'backslashreplace' not behaving as expected - why? Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-08-08 01:19 +0000
    Re: open: 'ascii', 'backslashreplace' not behaving as expected - why? Veek M <veekjunk@foobar.com> - 2026-08-08 04:22 +0000
      Re: open: 'ascii', 'backslashreplace' not behaving as expected - why? Veek M <veekjunk@foobar.com> - 2026-08-08 04:25 +0000
        Re: open: 'ascii', 'backslashreplace' not behaving as expected - why? Veek M <veekjunk@foobar.com> - 2026-08-08 04:33 +0000
          Re: open: 'ascii', 'backslashreplace' not behaving as expected - why? Greg Ewing <greg.ewing@canterbury.ac.nz> - 2026-08-10 12:08 +1200

csiph-web