Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any python "utf-8" string constant?

I would like to use "utf-8" string constant because it is always confusing for me to choose between "UTF-8", "UTF8", "utf8", "utf-8", "utf-8", "utf_8"

All code samples in python documentation have syntax like:

with io.open("/tmp/a.txt", "w", encode="utf-8") as file_cursor:
    file_cursor.write(text)

Can somebody tell me why is like this, maybe some best practice, zends ... ?

I would like to use code suggestion in IDE to achieve something like:

with io.open("/tmp/a.txt", "w", encode=ENCODINGS.UTF8) as file_cursor:
    file_cursor.write(text)  

Is there any standard constant out of the box in python core?

like image 499
Andrei.Danciuc Avatar asked May 22 '17 09:05

Andrei.Danciuc


1 Answers

You could use:

import encodings

encodings.utf_8.getregentry().name

which gives 'utf-8'.

like image 156
Simeon Visser Avatar answered Oct 02 '22 16:10

Simeon Visser