gpt4 book ai didi

python - 字符串中出现非 ASCII 字符

转载 作者:太空宇宙 更新时间:2023-11-04 06:40:06 25 4
gpt4 key购买 nike

所以我需要比较 2 个 strings :

str1 = 'this is my string/ndone'
str2 = 'this is my string done'

所以我将 str1 中的新行替换为 ' ':

new_str = str1.replace('\n', ' ')

当打印 2 个字符串时,它们是相同的:

'this is my string done'

但是当使用 == 运算符进行比较时,我将这 2 个字符串转换为数组以查看它们为什么不相等:

arr1 = bytearray(str1 , 'utf-8')
print(arr1)

arr2 = bytearray(str2 , 'utf-8')
print(arr2)

这是输出:

str1 = bytearray(b'this is\xc2\xa0my string done')

str2 = bytearray(b'this is my string done')

那么这个 \xc2\xa0 是什么?

最佳答案

'\xc2\xa0'是Unicode字符'NO-BREAK SPACE' (U+00A0)的UTF-8编码.

关于python - 字符串中出现非 ASCII 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57163854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com