简体   繁体   English

如何比较 Python 中包含反斜杠的两个字符串

[英]How to compare two strings which contain backslashes in Python

I am working with the module ocr from Python and some image return this string '921,\”' .我正在使用 Python 中的模块 ocr 并且一些图像返回此字符串'921,\”' (The image is a date which looks like 9/21/2015) (图像是一个日期,看起来像 9/21/2015)

Now, if I try to execute this现在,如果我尝试执行此操作

a == '921,\u201d'

Python automatically escapes the inverted slash ( \\ ) in the hardcoded string and the comparison is not equal. Python 会自动转义硬编码字符串中的反斜杠 ( \\ ),并且比较不相等。

How can I compare two ascii strings without Python interpreting some substrings as unicode characters?如何在没有 Python 将某些子字符串解释为 unicode 字符的情况下比较两个 ascii 字符串?

The length is 6 The error is self.assertIn(res0, [r'921,\”', "Feb 21, 2015"]) AssertionError: u'(921,\”' not found in ['921,\”', 'Feb 21, 2015']长度为 6 错误为 self.assertIn(res0, [r'921,\”', "Feb 21, 2015"]) AssertionError: u'(921,\”' not found in ['921,\”' , '2015 年 2 月 21 日']

Thanks.谢谢。 \\u201 is a only one char. \\u201 只有一个字符。

The easiest way is to use a raw string literal.最简单的方法是使用原始字符串文字。

a == r'921,\u201d'

This allows you to use literal backslashes without having to escape them (the only restriction is that a string still can't end with an unescaped backslash).这允许您使用文字反斜杠而不必转义它们(唯一的限制是字符串仍然不能以未转义的反斜杠结尾)。

You can also escape the backslash by prepending a backslash to it:您还可以通过在其前面添加反斜杠来转义反斜杠:

a == '921,\\u201d'

Try to decode using 'utf-8':尝试使用 'utf-8' 解码:

>>> '921,\u201d'.decode('utf-8')
u'921,\\u201d'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM