简体   繁体   中英

Replacing a character in a python string - odd output with Print

I'm trying to make some code that allows me to replace a character in a list where the position is defined by an integer. I have the following code

# -*- coding: utf-8 -*-
a = 47
text = 'xxxxxxxxxx xxxxx ╟───┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼───╢ xx'
new = list(text)
new[a] = "x"
print ''.join(new)

But when I run it, it prints out

xxxxxxxxxx xxxxx ╟───┼────┼x▒▒───┼────┼────┼────┼────┼────┼────┼────┼────┼───╢ xx

Instead of

xxxxxxxxxx xxxxx ╟───┼────┼x──┼────┼────┼────┼────┼────┼────┼────┼────┼───╢ xx

In other words it includes the "▒▒" in the printed string. It adds extra characters regardless of which character is replaced in the list. What am I doing wrong?

I'm running it on a raspberry pi connected via SSH using putty.

Since you're using utf-8 encoding for the text (with non-ascii chars), you need to convert it to Unicode string on Python 2 so each char is not split into multiple bytes** before changing to a list.

Better yet, you can directly define the the text as unicode string:

text = u'xxxxxxxxxx xxxxx╟───┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼───╢ xx'

** There are cases where even unicode strings split chars into more than 1 element, but this won't affect you since you're within BMP range. If you want to know more, read about UCS-2 vs UCS-4 representations

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