How to detect ASCII characters on a string in python
Question: I'm working on a tool in maya where at some point, the user can enter a
comment on the textField. This comment will later be used as part of the
filename that's gonna be saved. I work in France so the user might use some
accentuated characters as "é" or "à".  
What i would love would be to just translate them to their non accentuated
corresponding character. However I realise this is quite tricky so I would be
ok with juste detecting them so I can issue a warning message to the user. I
don't want to just strip the incriminated letters as it might result on the
comment to be incomprensible.  
I know they're some similar questions around here, but they're all on other
languages I don't know/understand (such as C++ or php).

Here's what I found so far around the web :

    
    
    import re
    comment = 'something written with some french words and numbers'
    if re.match(r'^[A-Za-z0-9_]+$', text):
        # issue a warning for the user
    

This first solution doesn't work because it considers accentuated characters
as acceptable.

I found this :

    
    
    ENGLISH_CHARS = re.compile('[^\W_]', re.IGNORECASE)
    ALL_CHARS = re.compile('[^\W_]', re.IGNORECASE | re.UNICODE)
    
    assert len(ENGLISH_CHARS.findall('_àÖÎ_')) == 0
    assert len(ALL_CHARS.findall('_àÖÎ_')) == 3
    

which I thought about using like this :

    
    
    ENGLISH_CHARS = re.compile('[^\W_]', re.IGNORECASE)
    if len(ENGLISH_CHARS .findall(comment)) != len(comment):
        # issue a warning for the user
    

but it only seems to work if the string is encapsulated within underscores.

I'm really sorry if this a duplicate of something I haven't found or
understood, but it's been driving me nuts.


Answer: The [unicode](https://docs.python.org/2/howto/unicode.html) command tries to
encode your string in the given encoding. It will default to ASCII and raise
an exception if it fails.

    
    
    try:
        unicode(filename)
    except UnicodeDecodeError:
        show_warning()
    

This only allows unaccented characters, which is maybe what you want.

If you already have an Unicode string, you have to change the encoding, which
will raise an UnicodeEncodeError.

    
    
    filename.encode("ASCII")
    

Example:

    
    
    >>> unicode("ää")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)
    

