GVKun编程网logo

在Python 2.6中使用unicode_literals有任何陷阱吗?(python采用unicode)

10

对于想了解在Python2.6中使用unicode_literals有任何陷阱吗?的读者,本文将提供新的信息,我们将详细介绍python采用unicode,并且为您提供关于Eclipse运行pytho

对于想了解在Python 2.6中使用unicode_literals有任何陷阱吗?的读者,本文将提供新的信息,我们将详细介绍python采用unicode,并且为您提供关于Eclipse 运行python代码报错: UnicodeDecodeError: ''utf8'' codec can''t decode byte、gethostbyaddr在Python3中引发UnicodeDecodeError、Python 3 smtplib使用Unicode字符发送、python ftplib 使用报错 UnicodeEncodeError: ''latin-1'' codec can''t encode characters的有价值信息。

本文目录一览:

在Python 2.6中使用unicode_literals有任何陷阱吗?(python采用unicode)

在Python 2.6中使用unicode_literals有任何陷阱吗?(python采用unicode)

我们已经获得了在Python 2.6下运行的代码库。为了准备Python 3.0,我们开始添加:

从__future__导入unicode_literals

进入我们的.py文件(我们对其进行修改)。我想知道是否还有其他人正在这样做并且遇到了任何非显而易见的陷阱(也许在花费大量时间进行调试之后)。

Eclipse 运行python代码报错: UnicodeDecodeError: ''utf8'' codec can''t decode byte

Eclipse 运行python代码报错: UnicodeDecodeError: ''utf8'' codec can''t decode byte

错误信息如下:
在Eclipse上面运行python脚本时报错
网上报这个错误的原因有很多,包括:

  1. 脚本本身不是使用UTF-8编码的,需要转成utf-8编码
  2. 脚本的头部需要声明为:utf-8 编码 脚本头部需要使用utf8编码
  3. 工程路径中包含中文字符也会报这个错误,因为python会读取windows中工程路径,而windows中的编码一般都是亲生的gbk类编码不太容易修改,所以这种情况下需要将你的工程移到没有中文字符的目录下,然后在重新导入.

gethostbyaddr在Python3中引发UnicodeDecodeError

gethostbyaddr在Python3中引发UnicodeDecodeError

问题描述

我正在用Python 3.4.1来构建一个Django项目。

 manage.py runserver 引发UnicodeDecodeException。

如何解决?

查看以下(修剪)追溯:

追溯(最近的最后一次呼叫):

文件C:\ Python34\lib\socketserver.py,第429行__init__  self.server_bind()

文件C:\Python34\lib\site-packages\django\core \servers\basehttp.py,第121行,在server_bind 超级(WSGIServer,self).server_bind()

文件C:\Python34\lib\wsgiref \simple_server.py,第50行,server_bind  HTTPServer.server_bind(self)

文件C:\Python34\lib\http\server.py,第135行, server_bind  self.server_name = socket.getfqdn(host)

文件C:\Python34\lib\socket.py,第460行,getfqdn 主机名,别名,ipaddrs = gethostbyaddr(name)

UnicodeDecodeError:''utf-8''编解码器无法解码位置12中的0xcf字节无效连续字节 

每个请求,我的主机名:

 C:\Users\AnatoliyVik>主机名 AnatoliyVik-ПК 

解决方案

将主机名更改为不再包含西里尔字符解决问题。

感谢@拉法达的指导。

I''m trying to build a Django project with Python 3.4.1. manage.py runserver raises a UnicodeDecodeException.

How can this be resolved? See the below (trimmed) traceback:

Traceback (most recent call last):
  File "C:\Python34\lib\socketserver.py", line 429, in __init__
    self.server_bind()
  File "C:\Python34\lib\site-packages\django\core\servers\basehttp.py", line 121
, in server_bind
    super(WSGIServer, self).server_bind()
  File "C:\Python34\lib\wsgiref\simple_server.py", line 50, in server_bind
    HTTPServer.server_bind(self)
  File "C:\Python34\lib\http\server.py", line 135, in server_bind
    self.server_name = socket.getfqdn(host)
  File "C:\Python34\lib\socket.py", line 460, in getfqdn
    hostname, aliases, ipaddrs = gethostbyaddr(name)
UnicodeDecodeError: ''utf-8'' codec can''t decode byte 0xcf in position 12: invalid
 continuation byte

Per request, my hostname:

C:\Users\AnatoliyVik>hostname
AnatoliyVik-ПК

Changing the hostname to no longer include Cyrillic symbols resolves the problem.

Thanks to @Lafada for the guidance.

这篇关于gethostbyaddr()在Python 3中引发UnicodeDecodeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

您可能感兴趣的文章:
  • python3的UnicodeDecodeError解决方法
  • Python运行报错UnicodeDecodeError的解决方法
  • Python UnicodedecodeError编码问题解决方法汇总

Python 3 smtplib使用Unicode字符发送

Python 3 smtplib使用Unicode字符发送

我在使用Python 3中的smtplib向unicode字符发送电子邮件时遇到问题,这在3.1.1中失败,但在2.5.4中有效:

  import smtplib
  from email.mime.text import MIMEText

  sender = to = 'ABC@DEF.com'
  server = 'smtp.DEF.com'
  msg = MIMEText('€10')
  msg['Subject'] = 'Hello'
  msg['From'] = sender
  msg['To'] = to
  s = smtplib.SMTP(server)
  s.sendmail(sender,[to],msg.as_string())
  s.quit()

我尝试了文档中的示例,但也失败了。 http://docs.python.org/3.1/library/email-
examples.html,以MIME消息示例的形式发送目录的内容

有什么建议?

python ftplib 使用报错 UnicodeEncodeError: ''latin-1'' codec can''t encode characters

python ftplib 使用报错 UnicodeEncodeError: ''latin-1'' codec can''t encode characters

高春辉、王春生、朱峰:关于开源创业的 15 件小事

-- 版本 python3

-- 模块:ftplib

 

使用 ftplib 上传文件的时候,遇到包含中文的文件名报错 UnicodeEncodeError: ''latin-1'' codec can''t encode charactersin position 4-5: ordinal not in range (256)

 

        更改 ftplib.py 里

        encoding =''latin-1''

        为

        encoding =''utf-8''

ftplib.py 文件一般在 python 安装目录的 Lib 文件夹下

我们今天的关于在Python 2.6中使用unicode_literals有任何陷阱吗?python采用unicode的分享就到这里,谢谢您的阅读,如果想了解更多关于Eclipse 运行python代码报错: UnicodeDecodeError: ''utf8'' codec can''t decode byte、gethostbyaddr在Python3中引发UnicodeDecodeError、Python 3 smtplib使用Unicode字符发送、python ftplib 使用报错 UnicodeEncodeError: ''latin-1'' codec can''t encode characters的相关信息,可以在本站进行搜索。

本文标签: