The original post started out with r'\x0a' but then talked about '\xdd'. I assumed that there was a pattern here, a raw string containing "\x" and two more characters, and made a suggestion for converting any string with that pattern. But the OP was very unclear what the task really was, so here we all are, making a variety of guesses.

On 12/8/2022 8:23 AM, Weatherby,Gerard wrote:
I’m not understanding the task. The sample code given is converting the input 
r’\x0a’ to a newline, it appears.


import re


def exam(z):
     print(f"examine {type(z)} {z}")
     for c in z:
         print(f"{ord(c)} {c}")

s0 = r'\x0a'

def to1byte(matchobj):
     return chr(int('0x' + matchobj.group(1), 16))
s1 = re.sub(r'\\x([0-9a-fA-F]{2})', to1byte, s0)
exam(s0)
exam(s1)

---
examine <class 'str'> \x0a
92 \
120 x
48 0
97 a
examine <class 'str'>

10

From: Python-list <python-list-bounces+gweatherby=uchc....@python.org> on behalf of 
Jach Feng <jf...@ms4.hinet.net>
Date: Wednesday, December 7, 2022 at 9:27 PM
To: python-list@python.org <python-list@python.org>
Subject: Re: How to convert a raw string r'xdd' to 'xdd' more gracefully?
*** Attention: This is an external email. Use caution responding, opening 
attachments or clicking on links. ***

Peter Otten 在 2022年12月8日 星期四清晨5:17:59 [UTC+8] 的信中寫道:
On 07/12/2022 03:23, Jach Feng wrote:
s0 = r'\x0a'
At this moment it was done by

def to1byte(matchobj):
....return chr(int('0x' + matchobj.group(1), 16))
s1 = re.sub(r'\\x([0-9a-fA-F]{2})', to1byte, s0)

But, is it that difficult on doing this simple thing?
import codecs
codecs.decode(r"\x68\x65\x6c\x6c\x6f\x0a", "unicode-escape")
'hello\n'
Thank you. What I really want to handle is to any r'\xdd'. The r'\x0a' is for 
example. Sorry, didn't describe it clearly:-)
--
https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!kUnextA7_cF7EoP_4hGzC5Jq2wRvn8nwLwT8wmeNkgVjK_n6VG19fxb-4SwmDMwepWe8_bGaH9Y2LlkSvFRz$<https://urldefense.com/v3/__https:/mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!kUnextA7_cF7EoP_4hGzC5Jq2wRvn8nwLwT8wmeNkgVjK_n6VG19fxb-4SwmDMwepWe8_bGaH9Y2LlkSvFRz$>

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to