相关疑难解决方法(0)

在Python中管道stdout时设置正确的编码

管道Python程序的输出时,Python解释器会对编码感到困惑,并将其设置为None.这意味着这样的程序:

# -*- coding: utf-8 -*-
print u"åäö"
Run Code Online (Sandbox Code Playgroud)

正常运行时会正常工作,但失败时:

UnicodeEncodeError:'ascii'编解码器无法对位置0中的字符u'\ xa0'进行编码:序数不在范围内(128)

当在管道序列中使用时.

在配管时使这项工作的最佳方法是什么?我可以告诉它使用shell/filesystem /无论使用什么编码吗?

到目前为止我看到的建议是直接修改你的site.py,或者使用这个hack对defaultencoding进行硬编码:

# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
print u"åäö"
Run Code Online (Sandbox Code Playgroud)

是否有更好的方法使管道工作?

python terminal encoding stdout python-2.x

327
推荐指数
7
解决办法
19万
查看次数

AttributeError:'module'对象没有属性'setdefaultencoding'

我尝试安装xadmin(这是一个django的插件,用于使用Twitter的bootstrap后台).但是当我运行我的项目时,我的PyCharm终端出现以下错误:

File "C:\Python34\lib\site-packages\xadmin\sites.py", line 10, in <module>
sys.setdefaultencoding("utf-8")
AttributeError: 'module' object has no attribute 'setdefaultencoding'
Run Code Online (Sandbox Code Playgroud)

这是xadmin插件中sites.py的源代码摘录:

import sys
from functools import update_wrapper
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.db.models.base import ModelBase
from django.views.decorators.cache import never_cache
from imp import reload

reload(sys)
sys.setdefaultencoding("utf-8")
Run Code Online (Sandbox Code Playgroud)

该项目使用python 3.4解释器和Django 1.7.1运行.xadmin的版本是0.5.0

我能做什么 ?

python django

18
推荐指数
2
解决办法
6万
查看次数

Python UnicodeDecodeError:'ascii'编解码器不能解码不在范围内的字节0xe2序数(128)

我正在尝试使用Python在Google App Engine中编写我的第一个应用程序(该应用程序的链接:http://contractpy.appspot.com/ - 它只是一个实验性应用程序).整个代码如下.

但是,当我提交数据时,我收到此错误(显示在日志中):

(...) line 265, in get "contractType":geted_contractType
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 949: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)

第265行在此if块中:

self.response.out.write(yourcontract % {"resident":geted_resident_name,
                                      "nacionality":geted_user_nacionality,
                                      "SSN":geted_user_SSN,
                                      "SSN_error":geted_SSN_error,
                                      "driverLicense":geted_user_driverLicense,
                                      "email":geted_user_email,
                                      "witness ":geted_witness ,
                                      "owner":geted_owner,
                                      "contractType":geted_contractType
                                      })
Run Code Online (Sandbox Code Playgroud)

我尝试进行更改,阅读类似的awnswers(像这样或者这样),但没有任何帮助.我无法弄清楚这段代码出了什么问题.有人能弄清楚是什么导致了这样的错误以及如何解决它?

我正在使用Python 2.7.

在此先感谢您的帮助!

# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the …
Run Code Online (Sandbox Code Playgroud)

python google-app-engine ascii asciiencoding

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

使用pip安装uwsgi时,'ascii'编解码器无法解码错误

我按照本教程设置了uwsgi:https://uwsgi.readthedocs.org/en/latest/tutorials/Django_and_nginx.html .我pip install uwsgi在virtualenv中运行,但问题如下:

Command /home/timyitong/superleagues/bin/python -c "import setuptools;__file__='/home/timyitong/superleagues/build/uwsgi/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-Z9h8Jn-record/install-record.txt --single-version-externally-managed --install-headers /home/timyitong/superleagues/include/site/python2.6 failed with error code 1 in /home/timyitong/superleagues/build/uwsgi
Traceback (most recent call last):
  File "/home/timyitong/superleagues/bin/pip", line 9, in <module>
    load_entry_point('pip==1.4', 'console_scripts', 'pip')()
  File "/home/timyitong/superleagues/lib/python2.6/site-packages/pip/__init__.py", line 148, in main
    return command.main(args[1:], options)
  File "/home/timyitong/superleagues/lib/python2.6/site-packages/pip/basecommand.py", line 169, in main
    text = '\n'.join(complete_log)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 54: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)

这似乎是一个解码问题,但如何解决?我的系统环境是:

Ubuntu 10.04
Django==1.5.1
South==0.8.1 …
Run Code Online (Sandbox Code Playgroud)

python django uwsgi

11
推荐指数
2
解决办法
3万
查看次数

Python:UnicodeDecodeError:“ascii”编解码器无法解码位置 0 中的字节 0xef:序号不在范围内(128)

我目前的 python 3 代码有问题。

 replace_line('Products.txt', line, tenminus_str)
Run Code Online (Sandbox Code Playgroud)

是我试图变成 utf-8 的行吗,但是当我尝试像其他人一样这样做时,我会收到错误,例如没有属性,并且当我尝试添加时,例如...

.decode("utf8")
Run Code Online (Sandbox Code Playgroud)

...到最后,我仍然收到它使用 ascii 的错误。我还尝试了与其他行一起使用的其他方法,例如添加 io. infront 并添加一个逗号

encoding = 'utf8'
Run Code Online (Sandbox Code Playgroud)

我用于 replace_line 的函数是:

def replace_line(file_name, line_num, text):
    lines = open(file_name, 'r').readlines()
    lines[line_num] = text
    out = open(file_name, 'w')
    out.writelines(lines)
    out.close()
Run Code Online (Sandbox Code Playgroud)

我将如何解决这个问题?请注意,我对 Python 非常陌生,还不够先进,无法很好地进行调试。

编辑:与“重复”不同的解决此问题的方法

编辑 2:我现在有另一个功能错误。

File "FILELOCATION", line 45, in refill replace_line('Products.txt', str(line), tenminus_str) 

File "FILELOCATION", line 6, in replace_line lines[line_num] = text

TypeError: list indices must be integers, not str 
Run Code Online (Sandbox Code Playgroud)

这是什么意思,我该如何解决?

python unicode ascii utf-8

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