Home Download Buy Blog Forum Support

Accents problems

Accents problems

Postby cadid on Fri May 04, 2012 2:33 pm

Hi i found some problemes with accents

OS: Windows 7
Sublime Text 2 build: 2181

Hi i found a problem with accents i try to create a plugin (with a little script i found around) to insert datetime in the code with a a shortcut

i create the insert_timestamp.py, the encoding of the file is utf8 but in the code i have accents:

Code: Select all
import datetime
import sublime_plugin

class insert_timestamp(sublime_plugin.TextCommand):
  def run(self, edit):
    #generate the timestamp
    timestamp_str = '# Dídac # ' + datetime.datetime.now().strftime("%Y-%m-%d %H:%M")

    #for region in the selection
    #(i.e. if you have multiple regions selected,
    # insert the timestamp in all of them)
    for r in self.view.sel():
      #put in the timestamp
      #(if text is selected, it'll be
      # replaced in an intuitive fashion)
      if r.size() > 0:
        self.view.replace(edit, r, timestamp_str)
      else:
        self.view.insert(edit, r.begin(), timestamp_str)


When i tried to save the plugin (ctrl + s) the console report:

Code: Select all
Traceback (most recent call last):
  File ".\sublime_plugin.py", line 62, in reload_plugin
  File ".\insert_timestamp.py", line 7
SyntaxError: Non-ASCII character '\xc3' in file .\insert_timestamp.py on line 7, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details


I solved introducing at the first line:
Code: Select all
# coding=utf-8


Now it saves correctly, but when i try to use the plugin

i use shift+alt+d to use it now shows:

Code: Select all
self.view.insert(edit, r.begin(), timestamp_str)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 4: ordinal not in range(128)


if i remove the accent char it works correct, it can be a bug, or maybe is problems with the last line of python code.

thanks
cadid
 
Posts: 3
Joined: Fri May 04, 2012 2:19 pm

Re: [BUG] Accents problems

Postby jps on Fri May 04, 2012 2:39 pm

Try declaring your accented character in a unicode string literal, e.g.,

Code: Select all
mystr = u"é"
jps
Site Admin
 
Posts: 3077
Joined: Wed Mar 19, 2008 12:33 pm

Re: [BUG] Accents problems

Postby cadid on Fri May 04, 2012 2:50 pm

jps wrote:Try declaring your accented character in a unicode string literal, e.g.,

Code: Select all
mystr = u"é"


same problem ):
cadid
 
Posts: 3
Joined: Fri May 04, 2012 2:19 pm

Re: [BUG] Accents problems

Postby jps on Sat May 05, 2012 2:13 pm

You've made an ascii string containing a unicode character, which won't work. You need to ensure you're using a unicode string. For example, this doesn't work:

Code: Select all
# coding=utf-8
import sublime, sublime_plugin

class BadInsertUnicode(sublime_plugin.TextCommand):
   def run(self, edit):
      self.view.insert(edit, 0, "é")

This does work:

Code: Select all
# coding=utf-8
import sublime, sublime_plugin

class InsertUnicode(sublime_plugin.TextCommand):
   def run(self, edit):
      self.view.insert(edit, 0, u"é")
jps
Site Admin
 
Posts: 3077
Joined: Wed Mar 19, 2008 12:33 pm

Re: Accents problems

Postby cadid on Mon May 07, 2012 7:18 am

Just working now, thanks a lot!

Less problems to solve now
cadid
 
Posts: 3
Joined: Fri May 04, 2012 2:19 pm


Return to Technical Support

Who is online

Users browsing this forum: No registered users and 20 guests