简体   繁体   English

如何将字符串识别为字节文字?

[英]How to identify a string as being a byte literal?

In Python 3, if I have a string such that: 在Python 3中,如果我有一个这样的字符串:

print(some_str)

yields something like this: 产生这样的东西:

b'This is the content of my string.\r\n'

I know it's a byte literal. 我知道这是字节字面量。

Is there a function that can be used to determine if that string is in byte literal format (versus having, say, the Unicode 'u' prefix) without first interpreting? 是否有一个函数可用于确定该字符串是否为字节文字格式(相对于具有'u'前缀)而无需先进行解释? Or is there another best practice for handling this? 还是有另一种最佳实践来处理此问题? I have a situation wherein getting a byte literal string needs to be dealt with differently than if it's in Unicode. 我遇到的一种情况是,与使用Unicode相比,处理字节的文字字符串需要以不同的方式处理。 In theory, something like this: 从理论上讲,是这样的:

if is_byte_literal(some_str):
    // handle byte literal case
else:
    // handle unicode case

The easiest and, arguably, best way to do this would be by utilizing the built-in isinstance with the bytes type: 做到这一点的最简单也是最好的方法是利用bytes类型的内置isinstance

some_str = b'hello world'
if isinstance(some_str, bytes):
    print('bytes')
elif isinstance(some_str, str):
    print('str')
else:
    # handle

Since, a byte literal will always be an instance of bytes , isinstance(some_str, bytes) will, of course, evaluate to True . 因为字节字面量将始终bytes的实例,所以isinstance(some_str, bytes)当然会求值为True

Just to complement the other answer, the built-in type also gives you this information. 只是为了补充其他答案,内置type还为您提供了此信息。 You can use it with is and the corresponding type to check accordingly. 您可以将其与is和相应的类型一起使用以进行检查。

For example, in Python 3: 例如,在Python 3中:

a = 'foo'
print(type(a) is str)   # prints `True`
a = b'foo'
print(type(a) is bytes) # prints `True` as well

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

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