小编Dav*_*d Q的帖子

使用 unicode 字符 u201c

我是 python 的新手,在理解 unicode 时遇到问题。我正在使用 Python 3.4。我花了一整天试图通过阅读有关 unicode 的信息来解决这个问题,包括http://www.fileformat.info/info/unicode/char/201C/index.htmhttp://python-notes.curiousefficiency.org /en/latest/python3/text_file_processing.html

我需要引用特殊引号,因为它们用于我正在分析的文本中。我确实测试过 W7 命令窗口可以读写 2 个特殊引号字符。为了简单起见,我写了一个单行脚本:

print ('“') # that's the special quote mark in between normal single quotes
Run Code Online (Sandbox Code Playgroud)

并得到这个输出:

Traceback (most recent call last):
  File "C:\Users\David\Documents\Python34\Scripts\wordCount3.py", line 1, in <module>
    print ('\u201c')
  File "C:\Python34\lib\encodings\cp437.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u201c' in position 0: character maps to <undefined>
Run Code Online (Sandbox Code Playgroud)

那么我如何写一些东西来引用这两个字符u201Cu201D

这是文件打开语句中的正确编码选择吗?

with open(fileIn, mode='r', encoding='utf-8', errors='replace') as f:
Run Code Online (Sandbox Code Playgroud)

python unicode encoding utf-8 python-3.x

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

encoding ×1

python ×1

python-3.x ×1

unicode ×1

utf-8 ×1