简体   繁体   English

Python:检查我的字符串是否包含非ASCII字符

[英]Python: Check whether my string contains non ascii chars

I work with dictionary and I want to detect keys which contains non-english characters like 'č' for example. 我使用字典,并且想检测包含非英语字符(例如“č”)的键。

This is my code: 这是我的代码:

for i in dictionaryMemory.keys():
            czechChars=['á','é','í','ó','ů','ě','ř','ť','č','š','ž']
            if any(s in i for s in czechChars):
                dictionaryMemory.pop(i)
                print(i)

But when I try to run this code, eclipse prints that SyntaxError: Non-ASCII character '\\xc3' in file... 但是,当我尝试运行此代码时,eclipse SyntaxError: Non-ASCII character '\\xc3' in file...打印出SyntaxError: Non-ASCII character '\\xc3' in file...

Ok, so I try to put # -*- coding: utf-8 -*- ¨. 好的,所以我尝试输入# -*- coding: utf-8 -*- ¨。 It seems to work, but it can't find any of these characters. 似乎可行,但找不到任何这些字符。 What to do? 该怎么办?

EDIT: In the project properties I found this: Text file encoding: inherited from container(Cp1250) 编辑:在项目属性中,我发现了这一点: Text file encoding: inherited from container(Cp1250)

I have a problems with encoding very usually but can't still figure out how to do it in a proper way... 我通常在编码方面遇到问题,但仍无法弄清楚如何以正确的方式进行编码...

就像Tal Kremerman和Mark Ransom所说的那样,我试图将编码注释从utf-8更改为cp1250 ,然后检查代码中的字符是否格式正确,所以没有,所以我将其更改为č,š,ř...终于可以了!

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

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