【问题标题】:Python encoding issue, can't seem to figure it outPython编码问题,似乎无法弄清楚
【发布时间】:2016-03-12 20:02:40
【问题描述】:

嘿,我在 python 中进行编码时遇到了这个主要问题。我对python不太熟悉,并且已经被这个错误困住了好几个星期。我觉得我已经尝试了所有可能的事情,但我似乎无法得到它。

我正在读取要使用的文件,但在某些包含中文字符的文件上出现以下错误。

 'ascii' codec can't encode characters in position 10314-10316: ordinal not in range(128)
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py", line 112, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/lib/python2.7/site-packages/cc_counter-0.65-py2.7.egg/cc_counter/views.py", line 154, in reviewrequest_recent_cc
    prev_reviewrequest_ccdata = _reviewrequest_recent_cc(request, review_request_id, False, revision_offset=1)
  File "/usr/lib/python2.7/site-packages/cc_counter-0.65-py2.7.egg/cc_counter/views.py", line 140, in _reviewrequest_recent_cc
    filename, comparison_data = _download_comparison_data(request, review_request_id, revision, filediff_id, modified)
  File "/usr/lib/python2.7/site-packages/cc_counter-0.65-py2.7.egg/cc_counter/views.py", line 89, in _download_comparison_data
    revision, filediff_id, local_site, modified)
  File "/usr/lib/python2.7/site-packages/cc_counter-0.65-py2.7.egg/cc_counter/views.py", line 68, in _download_analysis
    temp_file.write(working_file)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 10314-10316: ordinal not in range(128)

我在这方面的代码如下所示:

working_file = get_original_file(filediff, request, encoding_list)

if modified:
    working_file = get_patched_file(working_file, filediff, request)

working_file = convert_to_unicode(working_file, encoding_list)[1]
logging.debug("Encoding List: %s", encoding_list)
logging.debug("Source File: " + filediff.source_file)

temp_file_name = "cctempfile_" + filediff.source_file.replace("/","_")
logging.debug("temp_file_name: " + temp_file_name)
source_file = os.path.join(HOMEFOLDER, temp_file_name)


logging.debug("File contents" + working_file)
#temp_file = codecs.open(source_file, encoding='utf-8')
#temp_file.write(working_file.encode('utf-8'))

temp_file = open(source_file, 'w')
temp_file.write(working_file)
temp_file.close()

注意注释掉的行。 工作文件永远不会为空。 记录的“编码列表”中的编码是

Encoding List: [u'iso-8859-15']

如有任何帮助,我们将不胜感激。经过连续 8 小时的调试 + 前两周,我必须休息一下。

【问题讨论】:

    标签: python encoding python-unicode


    【解决方案1】:

    错误表明 working_file 是一个 Unicode 字符串,但正在写入一个打开的文件,该文件需要一个字节字符串。 Python 2 使用默认的ascii 编解码器将Unicode 字符串隐式转换为字节字符串,非ASCII 字符触发UnicodeEncodeError

    注释行接近正确,但write 将期望带有codecs.open 的Unicode 字符串,因此无需显式编码,并且需要打开文件进行写入:

    temp_file = codecs.open(source_file, 'w', encoding='utf-8')
    temp_file.write(working_file)
    

    【讨论】:

    • 我们不应该建议io.open 获得适当的换行支持吗?
    【解决方案2】:

    convert_to_unicode 函数的返回类型是什么?

    如果是字节,你可能应该将temp_file = open(source_file, 'w')更改为temp_file = open(source_file, 'wb'),这意味着将字节写入文件。

    【讨论】:

      猜你喜欢
      • 2011-07-08
      • 2021-04-26
      • 2021-06-04
      • 2023-02-11
      • 2014-07-28
      • 1970-01-01
      • 2014-05-19
      • 1970-01-01
      • 2015-12-31
      相关资源
      最近更新 更多