Menu

editor.replace complains about non ascii char

Help
Anonymous
2011-03-28
2013-01-25
  • Anonymous

    Anonymous - 2011-03-28

    when I try to do :

    editor.replace("’","’")
    

    I get this error in the console:

     File "C:\Users\xxxxxxxxxxx\AppData\Roaming\Notepad++\plugins\Config\PythonScript\scripts\cleanhtml.py", line 14
    SyntaxError: Non-ASCII character '\x92' in file C:\Usersxxxxxxx\AppData\Roaming\Notepad++\plugins\Config\PythonScript\scripts\cleanhtml.py on line 14, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
    

    I added:

    # -*- coding: utf-8 -*-
    

    to the top of the script,

    When I try it again, it doesn't replace it, on utf-8 documents, but does so on ANSI ones. (encoding menu on NPP)

    I tried doing:

    editor.replace('\x92','’')
    

    But it doesn't work either…

    How should this be done?

    Thanks,

     
  • Anonymous

    Anonymous - 2011-03-28

    Ahh I spoke too soon, in the documentation:

    Working with Unicode Text
    Python 2.x works with multi-byte strings, as does Scintilla. That means in most cases, you don’t need to do anything special to deal with unicode data, as both sides are talking the same language. However, there are a few things to observe, and occassionaly you’ll need to do something special to achieve what you want to do. One important point is to make sure your script is saved in the same encoding as your target file(s) - this helps unicode strings be interpreted the same way.
    If you need to work with the string (for instance chance the case), you need to convert the string to a Python Unicode string. To convert the string append .decode('utf8') to the string. Obviously if your string is in a different format, use the name of the correct encoding.
    To put text back to Scintilla (so editor.something()), use .encode(‘utf8’) from a unicode string.
    

    I just changed the encoding on my script to utf-8 (the document was already utf-8) and it worked :O.

    Sorry, I didn't search the help file before submitting :D

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.