Sublime Forum

Accents problems

#1

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:

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:

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:

# 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:

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

0 Likes

#2

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

mystr = u"é"
0 Likes

#3

[quote=“jps”]Try declaring your accented character in a unicode string literal, e.g.,

mystr = u"é" [/quote]

same problem ):

0 Likes

#4

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:

# coding=utf-8
import sublime, sublime_plugin

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

This does work:

# coding=utf-8
import sublime, sublime_plugin

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

#5

Just working now, thanks a lot!

Less problems to solve now

0 Likes