【问题标题】:Non latin characters code generated by QtDesignerQtDesigner 生成的非拉丁字符代码
【发布时间】:2022-12-07 05:21:37
【问题描述】:

我由 Qt Designer python 代码生成(我使用西里尔字母),我得到了这个定义

self.gui_lang_label.setText(QCoreApplication.translate("MainWindow", u"\\u042f\\u0437\\u044b\\u043a \\u0438\\u043d\\u0442\\u0435\\u0440\\u0444\\u0435\\u0439\\u0441\\u0430", None))

如何将 u-string 转换为具有常规字符的字符串?

Qt Designer 能否生成带有常规字符的 python 代码 (PySide2)。我试着写一个脚本来修复这些字符串

【问题讨论】:

    标签: python-3.x qt-designer pyside2 cyrillic non-latin


    【解决方案1】:

    您显示的字符串 u"u042fu0437u044bu043a u0438u043du0442u0435u0440u0444u0435u0439u0441u0430" 是使用 Python 的 unicode-escape 编解码器编码的 Unicode 字符串。此编解码器使用反斜杠后跟十六进制形式的 Unicode 代码点对 Unicode 字符进行编码。

    要将此字符串转换为具有相应 Unicode 字符的常规字符串,可以使用 decode 方法并指定 unicode-escape 编解码器作为编码。例如,您可以使用以下代码来转换字符串:

    u_string = u"\u042f\u0437\u044b\u043a \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430"
    
    # Decode the Unicode string using the `unicode-escape` codec
    regular_string = u_string.decode("unicode-escape")
    
    # Print the decoded string
    print(regular_string)
    

    此代码使用 unicode-escape 编解码器解码 Unicode 字符串,并打印解码后的字符串,在本例中应为“Язык интерфейса”。

    或者,您也可以使用 codecs 模块来解码字符串。该模块提供了一个解码函数,您可以使用该函数使用指定的编解码器对字符串进行解码。以下是如何使用 codecs.decode 函数解码 Unicode 字符串的示例:

    import codecs
    
    u_string = u"\u042f\u0437\u044b\u043a \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430"
    
    # Decode the Unicode string using the `unicode-escape` codec
    regular_string = codecs.decode(u_string, "unicode-escape")
    
    # Print the decoded string
    print(regular_string)
    

    此代码还使用 unicode-escape 编解码器解码 Unicode 字符串并打印解码后的字符串。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-26
      • 1970-01-01
      • 2015-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多