为什么使用python split时字符串会发生变化? [英] Why does the string change when using python split?

查看:42
本文介绍了为什么使用python split时字符串会发生变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

test_str = "问题:4 月 23 日星期一,世界标准时间 07:00 在 Bitfinex 交易所,加密货币比特币现金(BCH/USD)结算价为 1368 美元.在您看来,BCH/USD 的交易价格是否高于1500 美元 (+9.65%) 在 Арril 28 之前的任何时间?指标:60.76%"打印(test_str)打印(test_str.split('之前'))

这是我拆分后得到的输出

"['问题:4 月 23 日星期一,世界标准时间 07:00 在 Bitfinex 交易所,加密货币比特币现金 (BCH/USD) 以 1368 美元结算.在您看来,BCH/USD 的交易价格是否高于1500 美元 (+9.65%) at an\xd1\x83 tim\xd0\xb5 b\xd0\xb5fore \xd0\x90\xd1\x80ril 28? Indic\xd0\xb0t\xd0\xber: 60.76%']"

演示:https://repl.it/repls/VitalOrganicBackups

解决方案

该问题是由拉丁字符和西里尔字符混合引起的.它们在大多数策略中的打印完全相同,但仍然是不同的字符,并且具有不同的代码.

问题中的输出适用于 Python 2.7(原始提问者使用的是什么),但很容易在 Python 3 中具有等效行为:

<预><代码>>>>打印(test_str.encode('UTF8'))b'问题:加密货币比特币现金(BCH/USD)于 UTC 时间 4 月 23 日星期一上午 07:00 在 Bitfinex 交易所以 1368 美元结算.在您看来,BCH/USD 的交易价格是否会高于 1500 美元(+9.65%)在\xd1\x83 tim\xd0\xb5 b\xd0\xb5fore \xd0\x90\xd1\x80ril 28?印度语\xd0\xb0t\xd0\xber: 60.76%'

unicodedata 模块有助于更好地理解实际发生的情况:

<预><代码>>>>对于 i 在 b'\xd1\x83\xd0\xb5\xd0\x90\xd1\x80\xd0\xbe'.decode('utf8'):打印(i, hex(ord(i)), i.encode('utf8'), unicodedata.name(i))

у 0x443 b'\xd1\x83' 西里尔小写字母 Uå 0x435 b'\xd0\xb5' 西里尔小写字母 IEА 0x410 b'\xd0\x90' 西里尔大写字母 Aр 0x440 b'\xd1\x80' 西里尔小写字母 ERо 0x43e b'\xd0\xbe' 西里尔小写字母 O

因此原始文本包含西里尔字母,为了进行比较,即使打印相同,它们与拉丁文的等价物也不相同.问题与 split 无关,只是一个糟糕的原始字符串.

test_str = "Question: The cryptocurrency Bitcoin Cash (BCH/USD) settled at 1368 USD at 07:00 AM UTC at the Bitfinex exchange on Monday, April 23. In your opinion, will BCH/USD trade above 1500 USD (+9.65%) at anу timе bеfore Арril 28? Indicаtоr: 60.76%"

print(test_str)
print(test_str.split('before '))

This the output I get after spliting

"['Question: The cryptocurrency Bitcoin Cash (BCH/USD) settled at 1368 USD at 07:00 AM UTC at the Bitfinex exchange on Monday, April 23. In your opinion, will BCH/USD trade above 1500 USD (+9.65%) at an\xd1\x83 tim\xd0\xb5 b\xd0\xb5fore \xd0\x90\xd1\x80ril 28? Indic\xd0\xb0t\xd0\xber: 60.76%']"

Demo: https://repl.it/repls/VitalOrganicBackups

解决方案

The problem is caused by a mix of Latin and Cyrillic characters. They print exactly the same in most policies, but are still different characters and do have different codes.

The output in the question is for Python 2.7 (what original question asker used) but it is easy to have equivalent behaviour in Python 3:

>>> print(test_str.encode('UTF8'))
b'Question: The cryptocurrency Bitcoin Cash (BCH/USD) settled at 1368 USD at 07:00 AM UTC at the Bitfinex exchange on Monday, April 23. In your opinion, will BCH/USD trade above 1500 USD (+9.65%) at an\xd1\x83 tim\xd0\xb5 b\xd0\xb5fore \xd0\x90\xd1\x80ril 28? Indic\xd0\xb0t\xd0\xber: 60.76%'

The unicodedata module helps to better understand what actually happens:

>>> for i in b'\xd1\x83\xd0\xb5\xd0\x90\xd1\x80\xd0\xbe'.decode('utf8'):
    print(i, hex(ord(i)), i.encode('utf8'), unicodedata.name(i))

у 0x443 b'\xd1\x83' CYRILLIC SMALL LETTER U
е 0x435 b'\xd0\xb5' CYRILLIC SMALL LETTER IE
А 0x410 b'\xd0\x90' CYRILLIC CAPITAL LETTER A
р 0x440 b'\xd1\x80' CYRILLIC SMALL LETTER ER
о 0x43e b'\xd0\xbe' CYRILLIC SMALL LETTER O

So the original text contains cyrillic letters and for comparisons, they are not the same of their latin equivalent, even if they print the same. The problem has nothing to do with split but is just a poor original string.

这篇关于为什么使用python split时字符串会发生变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆