简体   繁体   English

UnicodeEncodeError:“ ascii”编解码器无法对不在范围内的字符进行编码(128)

[英]UnicodeEncodeError: 'ascii' codec can't encode characters ordinal not in range(128)

I can't read the word Curaçao from a text file. 我无法从文本文件中读取Curaçao一词。 What am I doing wrong? 我究竟做错了什么?

I have written a text file that contains the word "Curaçao". 我写了一个包含单词“Curaçao”的文本文件。 The encoding on the editor (vim) is latin1. 编辑器(vim)上的编码为latin1。

This python program reads the file: 这个python程序读取文件:

import sys

with open ('foo.txt', 'r', encoding='latin1') as f:
    print('f:', f.encoding)
    print('stdout:', sys.stdout.encoding)
    for i in f:
        print(i)

And when I run it I get this... 当我运行它时,我得到了...

sundev19:/home/jgalloway12/code/wdPhone $ python3 CountryFix.py
f: latin1
stdout: 646
Traceback (most recent call last):
  File "CountryFix.py", line 11, in <module>
    print(i)
UnicodeEncodeError: 'ascii' codec can't encode character '\xe7' in position 4: ordinal not in range(128)

Here is the file's contents in binary. 这是二进制文件的内容。

0000000: 4375 7261 e761 6f0a                      Cura.ao.

EDIT: The "real" problem I am trying to solve here is reading an Excel 2010 exported CSV which contains country names. 编辑:我要在这里解决的“实际”问题是阅读包含国家名称的Excel 2010导出的CSV。

Fixed the file to be encoded in Latin1. 修复了要以Latin1编码的文件。 Program now prints locale. 程序现在将打印区域设置。

The problem here isn't the file, but the output stream. 这里的问题不是文件,而是输出流。

For whatever reason, python has detected your stdout encoding as US-ASCII when you really want something more (utf-8, latin1, etc.). 无论出于何种原因,当您真的想要更多东西(utf-8,latin1等)时,python都会将您的标准输出编码检测为US-ASCII。

Your options are: 您的选择是:

Trick it into believing a different encoding (on linux you can do this with LANG=en_US.UTF-8 , however I assume you're on windows and I don't recall how to trick python on windows in this way :)). 欺骗它,使其相信一种不同的编码(在Linux上,您可以使用LANG=en_US.UTF-8 ,但是我假设您在Windows上,并且我不记得如何以这种方式在Windows上欺骗python了:))。

Write your response to a file: 将您的回复写入文件:

with open('output.txt', 'w', encoding='latin1') as f:
    ...

Or write to the stdout bytestream: 或写入标准输出字节流:

import sys
sys.stdout.buffer.write(i.encode('latin1'))

Since you are printing the lines and python print function doesn't use of the encoding of open() function it tries to encode your string with it's default encoding which is ASCII. 由于您正在打印行,而python print函数不使用open()函数的编码,因此它会尝试使用默认编码ASCII来对字符串进行编码。 So you need to define a costume encoding for your unicode when you want to print it. 因此,当您要打印unicode时,需要为其定义服装编码。

You can use str.encode() method with a proper encocding for print. 您可以将str.encode()方法与正确的编码一起使用以进行打印。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python mmh3:UnicodeEncodeError:&#39;ascii&#39;编解码器无法在位置0-14处编码字符:序数不在范围内(128) - Python mmh3: UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-14: ordinal not in range(128) UnicodeEncodeError:&#39;ascii&#39;编解码器无法在位置0-3处编码字符:序数不在范围(128)中? - UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)? UnicodeEncodeError:“ ascii”编解码器无法对位置10-11中的字符进行编码:序数不在范围内(128) - UnicodeEncodeError: 'ascii' codec can't encode characters in position 10-11: ordinal not in range(128) Python:UnicodeEncodeError:&#39;ascii&#39;编解码器无法在位置34-39处编码字符:序数不在范围内(128) - Python: UnicodeEncodeError: 'ascii' codec can't encode characters in position 34-39: ordinal not in range(128) Canopy UnicodeEncodeError:“ ascii”编解码器无法对位置31-32中的字符进行编码:序数不在范围内(128) - Canopy UnicodeEncodeError: 'ascii' codec can't encode characters in position 31-32: ordinal not in range(128) UnicodeEncodeError:&#39;ascii&#39;编解码器不能编码位置0-6的字符:序数不在范围内(128) - UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-6: ordinal not in range(128) UnicodeEncodeError: &#39;ascii&#39; 编解码器无法对位置 0-9 中的字符进行编码:序号不在范围内 (128) - UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-9: ordinal not in range(128) UnicodeEncodeError:&#39;ascii&#39;编解码器无法对位置4273-4279中的字符进行编码:序数不在范围内(128) - UnicodeEncodeError: 'ascii' codec can't encode characters in position 4273-4279: ordinal not in range(128) UnicodeEncodeError:“ ascii”编解码器无法对位置26-27中的字符进行编码:序数不在范围内(128) - UnicodeEncodeError: 'ascii' codec can't encode characters in position 26-27: ordinal not in range(128) Python2.7 UnicodeEncodeError:&#39;ascii&#39;编解码器不能编码0-11位的字符:序号不在范围内(128) - Python2.7 UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-11: ordinal not in range(128)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM