【问题标题】:Using the python chr() function with combining diacritics结合变音符号使用 python chr() 函数
【发布时间】:2014-04-27 01:01:46
【问题描述】:

我正在编写一个 Python 脚本来为字符串添加变音符号(在 U+03XX 范围内)。但是,chr() 每次尝试将该范围内的数字转换为字符时都会抱怨。 (这是 Python 3,所以没有 unichr。)

这是我的代码的截断版本:

    strl = list(input())
    args = input().split(" ")
    strl[int(args[0])]+=chr(int(args[1])+0x300) #this is the line that breaks

它崩溃并出现以下异常:

UnicodeEncodeError: 'charmap' codec can't encode character '\u0300' in position 16: character maps to <undefined>

那么如何将整数转换为组合变音符号的字符串表示形式?

(例如,如果我将字符串"accent marks" 放入strl,将"0 0" 放入args,我希望strl["à","c","c","e","n","t"," ","m","a","r","k","s"] 结尾

更新:在 cmets 中,delnan 向我解释说 chr() 工作正常,但程序拒绝打印字符串。如何打印包含此代码块中字符的字符串(已存储在变量中)?

【问题讨论】:

  • 请给出strlargs 的一些示例值。
  • 具体是哪个版本的 Python?它适用于 3.3.2。
  • 异常类型和消息表明它正确创建了 U+0300 代码点,只是未能将其编码为某种编码(例如,写入字节流时)。回溯指向哪一行?
  • 您的控制台不支持打印该范围内的字符。如果它是 Unix 控制台,您可能缺少一些语言环境设置。如果它是一个 Windows 控制台,那么 MS C 标准库就不是 Unicode。

标签: python python-3.x unicode


【解决方案1】:

这里的问题不在于chr();问题只是 Windows 控制台无法显示有问题的字符。感谢所有在 cmets 中帮助我解决这个问题的人。

【讨论】:

    猜你喜欢
    • 2021-11-23
    • 2011-11-07
    • 1970-01-01
    • 2014-07-15
    • 2016-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-31
    相关资源
    最近更新 更多