简体   繁体   中英

Remove \r\n' from string in python 3

Given this string

b'141\r\n'

got by serial.readlines() , how can I extrapolate 141, or however the number in its position? Actually I can remove b' simply by using string[2:] but I cannot remove the remainder in this way. string.replace does not work I don't know why.

The number of figures in the number may vary. Is there a smart way to avoid this problem?

Ok so theres two questions here. Is your object

b = b'141\r\n'

or

s = "b'141\\r\\n'"

If it's the former

b.decode().strip()

if it's the latter

import re
re.search(r'\d+', s).group(0)

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