Sublime Forum

Get file encoding

#1

Hi,

view.encoding() returns the encoding set to a view, in a form that is not sensible for Python. It is just a string describing the encoding, like “Western (Windows 1252)”, instead of cp1252 which can be used as is in code. Even further, when encoding is not available, this function returns “Undefined”, like if I’m coding JavaScript.

I can try to manually write a transfer function, but it seems it would be relevant just for platform I’m currently using.

Is there any reference table, so that I can actually use this function in Python code and be sure that I’ll get right encoding regardless platform?

0 Likes

#2

what do you need the encoding for? when using view.substr, you always get a Python 3 string, which is unicode and thus will have the correct character contents.

1 Like

#3

I need it for a subprocess pipe, where I pass bytes.

0 Likes

#4
bytes(view.substr(sublime.Region(0, view.size())), 'UTF-8')

should work, regardless of the file’s encoding

2 Likes

#5

Thanks for your suggestion, I might as well go with universal_newlines and use strings in pipes instead trying possible scenarios with bytes encondings.

Cheers

0 Likes

#6

Just to add that using bytes in pipes may be still more desired option, instead strings with universal_newlines, because in later case Python uses stream encoded in locale.getpreferredencoding(False), that on Windows is cp1252, and I guess on other platforms it is ‘utf-8’.

0 Likes

#7

As always with character encodings, it is best if you know exactly what encoding the other party is using. You may get problems on Windows since they also have a concept of OEM encoding (my command line uses cp850 by default, for example).

0 Likes