简体   繁体   中英

Decode ASCII string in Python 3

Got a string in ASCII (with only ASCII value ranges for AZ, az, and " "), want to decode it.

ex. "781059910132" cooresponds to "Nice "

Is there an easy way to do this in Python 3?

You can use regular expressions to extract 3- or 2-digit combinations:

import re
ascii_char = '[01]?\d\d'
s = '781059910132'
''.join(map(chr, map(int, re.findall(ascii_char, s))))
#'Nice '

This code works even with 0-padded codes:

''.join(map(chr, map(int, re.findall(ascii_char, '07832078'))))
#'N N'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM