Sat*_*tya 1 python unicode ascii python-unicode
就我而言,当我使用时,我将 Unicode text_string 和前缀作为 ASCII 字符串
text-string.startswith(prefix)
Run Code Online (Sandbox Code Playgroud)
我以这种方式得到一个例外
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 9: ordinal not in range(128)
如何比较两个字符串,我尝试使用unicode(string)将 ASCII 字符串转换为 Unicode方法但仍然遇到相同的异常。
如何解决这个问题?在最坏的情况下如何在比较时抑制此异常?
text - u'PreciChrom I/II is a lyophilized control based on human citrated plasma.'
prefix - 'Reagents – working solutions'
Run Code Online (Sandbox Code Playgroud)
您的前缀字符串不是ASCII。正如错误消息所说,您在第 9 位有一个非 ASCII 字符;破折号,–
。该字符串可能是 utf-8。
您可以将前缀解码为 unicode:
text_string.startswith(prefix.decode('utf-8'))
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1396 次 |
最近记录: |