Sublime Text plugin anaconda automatically completes and solves Chinese garbled characters

New versions of this anaconda plugin only works with Python >= 3.6 and requires Sublime Text 4 build >= 4107。This means versions higher than 2.2.3 will not work in Sublime Text 3

 Configure custom anaconda Preference–Package Settings–Anaconda–Settings User

{
    "python_interpreter": "c:\\Python38\\python.exe",
    //由于Anaconda插件本身无法知道Python安装的路径,所以需要设置Python主程序的实际位置
    //忽略各种空格不对, 超过79字,没怎么遇到
    "pep8_ignore": ["E501", "W292", "E303", "W391", "E225", "E302", "W293", "E402"],
    //import的库没使用
    "pyflakes_explicit_ignore":
    [
        "UnusedImport"
    ],
    //保存文件后自动pep8格式化
    "auto_formatting": true,
    "auto_formatting_timeout": 5,
    //库函数的提示
    "enable_signatures_tooltip": true,
    "enable_docstrings_tooltip": true,
    "merge_signatures_and_doc": true,

    //ST3也有自动补全提示,但只提示文件中输入过的单词,这个功能可用提示变量可用的函数等。
    "suppress_word_completions": true,
    "suppress_explicit_completions": true,
    //会在函数内自动补全变量
    "complete_parameters": false,
    //代码排版时,行的默认长度太短,根据喜好设置
    "pep8_max_line_length": 120,
    "anaconda_linting": false,
}

Description: ( Settings of the sublime plugin anaconda [easy to understand]-Tencent Cloud Developer Community-Tencent Cloud )

  1. "python_interpreter": Set the path of the Python interpreter to ensure that the plugin can correctly find the Python interpreter.

  2. "pep8_ignore": Specifies a list of PEP 8 rules to ignore. For the rules you list (such as E501, W292, etc.), the plugin will not display related warnings or errors.

  3. "pyflakes_explicit_ignore": Specifies a list of pyflakes warning types to ignore. In this case, the plugin will not show unused import warnings.

  4. "auto_formatting"and "auto_formatting_timeout": Configure the auto-formatting option, allowing PEP 8 formatting to be applied automatically after saving a file.

  5. "enable_signatures_tooltip"and "enable_docstrings_tooltip": Enable signature hinting and docstring hinting for functions.

  6. "merge_signatures_and_doc": Signature hints and docstring hints for merged functions.

  7. "suppress_word_completions"and "suppress_explicit_completions": Suppress word completion and explicit completion.

  8. "complete_parameters": Configure whether to automatically complete variables within the function.

  9. "pep8_max_line_length": Configure the maximum length of each line in the PEP 8 specification.

  10. "anaconda_linting": Disables the code inspection feature of the Anaconda plugin.

----------------------

Use the sys.stdout.reconfigure() method to reconfigure the standard output encoding.
By setting the encoding to 'utf-8', you can ensure that standard output handles and displays Chinese characters correctly.
import sys
sys.stdout.reconfigure(encoding='utf-8')

Use the sys.stdout stream object in the io module to create a new output stream and set its encoding to UTF-8. Then use sys.stdout to refer to the new output stream.
By creating a new output stream and setting the encoding to UTF-8, you can ensure that Chinese characters can be displayed correctly.
import sys
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

Sublime Text sets the file encoding to a specific encoding method
set file encoding to If you are dealing with files containing Chinese characters, UTF-8 encoding is generally recommended because it supports a wide range of characters and is widely used in internationalized text use. Note that changing the file encoding will affect how Sublime Text interprets and displays the file. Make sure to select the appropriate encoding that matches the content of the file.

-----------------------

UTF-8 encoding supports Chinese characters.
UTF-8 (Unicode Transformation Format-8) is a variable-length Unicode encoding method that can represent almost all Unicode characters, including Chinese characters.

UTF-8 encoding uses 1 to 4 bytes to represent different characters, including ASCII characters (1 byte) and other non-ASCII characters (2 to 4 bytes). For Chinese characters, UTF-8 encoding usually uses 3 bytes to represent.

Since UTF-8 is a widely used encoding method, it is supported by almost all modern operating systems and applications. Therefore, when dealing with text or files containing Chinese characters, it is usually a good choice to use UTF-8 encoding to ensure that Chinese characters can be saved, transmitted and displayed correctly.

In Sublime Text, choosing UTF-8 encoding is a common practice when dealing with files containing Chinese characters, to ensure that Chinese content is interpreted and displayed correctly.

-----------------------------------------
Detect file encoding
result = chardet.detect(content)

import chardet

def read_file(file_path):
    with open(file_path, 'rb') as f:
        content = f.read()

    # 检测文件编码
    result = chardet.detect(content)
    encoding = result['encoding']
    confidence = result['confidence']
    print('encoding检测到的文件编码方式:',encoding,'\n')
    print('confidence检测置信度:',confidence,'\n')
    print('--------------')

    # 根据检测到的编码方式解码文件内容
    decoded_content = content.decode(encoding)

    return decoded_content

# 调用read_file函数读取文件并获取解码后的内容
file_path = 't检验.py'
content = read_file(file_path)

# 处理解码后的文件内容,例如进行字符串操作或文本分析
# ...

# 输出解码后的内容
print(content)

Guess you like

Origin blog.csdn.net/book_dw5189/article/details/131627944