如何检查Python中的字符串是否为ASCII? | 您所在的位置:网站首页 › python获得ascii › 如何检查Python中的字符串是否为ASCII? |
我想你不是在问正确的问题 - python中的字符串没有与'ascii',utf-8或任何其他编码对应的属性。你的字符串的来源(无论你是从文件中读取,从键盘输入等)都可能在ascii中编码了一个unicode字符串来生成你的字符串,但这就是你需要去寻找答案的地方。 也许你可以问的问题是:“这个字符串是在ascii中编码unicode字符串的结果吗?” - 您可以通过尝试来回答: try: mystring.decode('ascii') except UnicodeDecodeError: print "it was not a ascii-encoded unicode string" else: print "It may have been an ascii-encoded unicode string" |
CopyRight 2018-2019 实验室设备网 版权所有 |