GVKun编程网logo

Python-UnicodeEncodeError:“ charmap”编解码器无法编码字符(pythonutf-8编码无法解码)

10

在这篇文章中,我们将带领您了解Python-UnicodeEncodeError:“charmap”编解码器无法编码字符的全貌,包括pythonutf-8编码无法解码的相关情况。同时,我们还将为您介绍

在这篇文章中,我们将带领您了解Python-UnicodeEncodeError:“ charmap”编解码器无法编码字符的全貌,包括pythonutf-8编码无法解码的相关情况。同时,我们还将为您介绍有关nginx – uWSGI / Emperor:UnicodeEncodeError:’ascii’编解码器无法编码字符、Python - UnicodeDecodeError:'charmap' 编解码器无法解码位置 229393 中的字节 0x81:: 字符映射到 、Python 3 UnicodeDecodeError:“ charmap”编解码器无法解码字节0x9d、Python 3 UnicodeEncodeError:’ascii’编解码器无法编码字符的知识,以帮助您更好地理解这个主题。

本文目录一览:

Python-UnicodeEncodeError:“ charmap”编解码器无法编码字符(pythonutf-8编码无法解码)

Python-UnicodeEncodeError:“ charmap”编解码器无法编码字符(pythonutf-8编码无法解码)

我正在尝试抓取一个网站,但这给我一个错误。

我正在使用以下代码:

import urllib.requestfrom bs4 import BeautifulSoupget = urllib.request.urlopen("https://www.website.com/")html = get.read()soup = BeautifulSoup(html)print(soup)

我收到以下错误:

File "C:\Python34\lib\encodings\cp1252.py", line 19, in encode    return codecs.charmap_encode(input,self.errors,encoding_table)[0]UnicodeEncodeError: ''charmap'' codec can''t encode characters in position 70924-70950: character maps to <undefined>

我该怎么做才能解决此问题?

答案1

小编典典

我通过添加将.encode("utf-8")其修复soup

那意味着print(soup)变成print(soup.encode("utf-8"))

答案2

小编典典

UnicodeEncodeError将抓取的网页内容保存到文件中时,我得到的是相同的。为了解决这个问题,我替换了以下代码:

with open(fname, "w") as f:    f.write(html)

有了这个:

import iowith io.open(fname, "w", encoding="utf-8") as f:    f.write(html)

使用io可以向后兼容Python 2。

如果只需要支持Python 3,则可以改用内置open函数:

with open(fname, "w", encoding="utf-8") as f:    f.write(html)

nginx – uWSGI / Emperor:UnicodeEncodeError:’ascii’编解码器无法编码字符

nginx – uWSGI / Emperor:UnicodeEncodeError:’ascii’编解码器无法编码字符

我在uwsgi / emeror / Nginx服务器上编码有很大问题.
我的应用程序开发为批处理excel文件处理.

我使用最新版本的烧瓶和烧瓶扩展,并使用flask-excel.

我的应用程序在Digital Ocean / ubuntu服务器上运行,我的配置文件是:

/etc/init/uwsgi.conf

description "uWsgi"
start on runlevel [2345]
stop on runlevel [06]
respawn

env UWsgi=/home/lukas/www/abissk/venv/bin/uwsgi
env LOGTO=/home/lukas/logs/abissk/emperor.log
exec $UWsgi --master --emperor /etc/uwsgi/vassals --die-on-term --uid www-data --gid www-data --logto $LOGTO  

/etc/uwsgi/vassals/abissk_uwsgi.ini

[uwsgi]
plugins = python
#pcre = True

#application's base folder
base = /home/lukas/www/abissk/

#enable-threads = true

#python module to import
app = wsgi
module = %(app) 

home = %(base)venv
pythonpath = %(base)

#socket file's location
socket = /home/lukas/www/abissk/%n.sock

#permissions for the socket file
chmod-socket = 644

#the variable that holds a flask application inside the module imported at line #6
callable = app

#location of log files
logto = /home/lukas/logs/abissk/%n.log

/home/lukas/www/abissk/wsgi.py

# -*- coding: utf-8 *-*
from app.app import create_app
from app.settings import ProdConfig

app = create_app(config_object=ProdConfig)

在/ etc / Nginx的/启用的站点 – / abissk_Nginx

server {
    listen 80;
    server_name benela.abis.sk; 
    charset     utf-8;
    client_max_body_size 75M;

    location / { try_files $uri @yourapplication; }
    location @yourapplication {
        include uwsgi_params;
        uwsgi_pass unix:/home/lukas/www/abissk/abissk_uwsgi.sock;
    }

}

这是我的问题:
使用命令启动应用程序时:

 ~/www/abissk/venv/bin/uwsgi --ini /etc/uwsgi/vassals/abissk_uwsgi.ini

并上传excel文件工作得很好但是

当使用emperor与完全相同的配置文件(在/etc/init/uwsgi.conf中)启动相同的应用程序时,应用程序工作正常,但是当上传excel文件到批处理时,我只看到消息:“502 Bad Gateway”并在我的日志中是:

 UnicodeEncodeError: 'ascii' codec can't encode character '\xfd' in position 31: ordinal not in range(128)

我尝试使用apt-get / pip3安装安装uwsgi并且场景相同:当没有皇帝直接运行应用程序时,工作正常;当使用emperor运行应用程序(具有相同配置)时,每个上传excel文件到我的应用程序以崩溃结束:/

谢谢你的回答

@H_301_63@
最佳答案@H_301_63@
将以下行添加到您的abissk_uwsgi.ini文件中以强制uwsgi使用UTF-8.

env LANG=en_US.utf8
env LC_ALL=en_US.UTF-8
env LC_LANG=en_US.UTF-8
@H_301_63@

总结

以上是小编为你收集整理的nginx – uWSGI / Emperor:UnicodeEncodeError:’ascii’编解码器无法编码字符全部内容。

如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。

flask

Python - UnicodeDecodeError:'charmap' 编解码器无法解码位置 229393 中的字节 0x81:: 字符映射到 <undefined>

Python - UnicodeDecodeError:'charmap' 编解码器无法解码位置 229393 中的字节 0x81:: 字符映射到

您打开的文件不是 utf-8 格式,请检查格式(编码)并使用该格式而不是 utf-8。

试试

  encoding=''utf-8-sig''

Python 3 UnicodeDecodeError:“ charmap”编解码器无法解码字节0x9d

Python 3 UnicodeDecodeError:“ charmap”编解码器无法解码字节0x9d

我想制作搜索引擎,并按照某些网络中的教程进行操作。我想测试解析html

from bs4 import BeautifulSoupdef parse_html(filename):    """Extract the Author, Title and Text from a HTML file    which was produced by pdftotext with the option -htmlmeta."""    with open(filename) as infile:        html = BeautifulSoup(infile, "html.parser", from_encoding=''utf-8'')        d = {''text'': html.pre.text}        if html.title is not None:            d[''title''] = html.title.text        for meta in html.findAll(''meta''):            try:                if meta[''name''] in (''Author'', ''Title''):                    d[meta[''name''].lower()] = meta[''content'']            except KeyError:                continue        return dparse_html("C:\\pdf\\pydf\\data\\muellner2011.html")

它得到错误

UnicodeDecodeError: ''charmap'' codec can''t decode byte 0x9d in position 867: character maps to <undefined>enter code here

我在网上看到了一些使用encode()的解决方案。但是我不知道如何在代码中插入encode()函数。谁能帮我?

答案1

小编典典

在Python 3中,文件会以文本(解码为Unicode)的形式为您打开。您无需告诉BeautifulSoup要解码的编解码器。

如果数据解码失败,那是因为您没有告诉open()调用文件读取文件时使用哪种编解码器;这是因为 使用encoding参数添加正确的编解码器:

with open(filename, encoding=''utf8'') as infile:    html = BeautifulSoup(infile, "html.parser")

否则,将使用系统默认的编解码器打开文件,该默认编解码器取决于操作系统。

Python 3 UnicodeEncodeError:’ascii’编解码器无法编码字符

Python 3 UnicodeEncodeError:’ascii’编解码器无法编码字符

我刚刚开始学习 Python,但我已经遇到了麻烦.
我有一个只有一个命令的简单脚本:
#!/usr/bin/env python3
print("Příliš žluťoučký kůň úpěl ďábelské ódy.") # Text in Czech

当我尝试运行此脚本时:

python3 hello.py

我收到这条消息:

Traceback (most recent call last):
  File "hello.py",line 2,in <module>
    print("P\u0159\xedli\u0161 \u017elu\u0165ou\u010dk\xfd k\u016fn \xfap\u011bl \u010f\xe1belsk\xe9 \xf3dy.")
UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-2: ordinal not in range(128)

我正在使用Kubuntu 16.04和Python 3.5.2.
当我尝试这个:导出PYTHONIOENCODING = utf-8它只能暂时工作.下次我打开bash时遇到了同样的错误.

根据https://docs.python.org/3/howto/unicode.html#the-string-type
Python源代码的默认编码是UTF-8.
所以我把源文件保存为id UTF-8,Konsole设置为UTF-8但我仍然得到错误!
即使我补充说

# -*- coding: utf-8 -*-

从一开始它什么都不做.

另一个奇怪的事情:当我只使用python而不是python3运行它时,它可以工作.如何在Python 2.7.12中工作而不是在3.5.2中工作?

永久解决这个问题的任何想法?谢谢.

解决方法

感谢Mark Tolen和Alastair McCormack建议问题所在.问题出在区域设置中.
当我运行语言环境时,输出是:
LANG=C
LANGUAGE=
LC_CTYPE="C"
LC_NUMERIC=cs_CZ.UTF-8
LC_TIME=cs_CZ.UTF-8
LC_COLLATE=cs_CZ.UTF-8
LC_MONETARY=cs_CZ.UTF-8
LC_MESSAGES="C"
LC_PAPER="C"
LC_NAME="C"
LC_ADDRESS="C"
LC_TELEPHONE="C"
LC_MEASUREMENT=cs_CZ.UTF-8
LC_IDENTIFICATION="C"
LC_ALL=

此“C”是使用ANSI charmap的默认设置.这就是问题所在.运行locale charmap给了我:ANSI_X3.4-1968,它不能显示非英文字符.
我用这个Ubuntu documentation site解决了这个问题.

我将这些行添加到/ etc / default / locale:

LANGUAGE=cs_CZ.UTF-8
LC_ALL=cs_CZ.UTF-8

然后,您必须重新启动会话(注销并进入)才能应用这些设置.

运行语言环境现在返回此输出:

LANG=C
LANGUAGE=cs
LC_CTYPE="cs_CZ.UTF-8"
LC_NUMERIC="cs_CZ.UTF-8"
LC_TIME="cs_CZ.UTF-8"
LC_COLLATE="cs_CZ.UTF-8"
LC_MONETARY="cs_CZ.UTF-8"
LC_MESSAGES="cs_CZ.UTF-8"
LC_PAPER="cs_CZ.UTF-8"
LC_NAME="cs_CZ.UTF-8"
LC_ADDRESS="cs_CZ.UTF-8"
LC_TELEPHONE="cs_CZ.UTF-8"
LC_MEASUREMENT="cs_CZ.UTF-8"
LC_IDENTIFICATION="cs_CZ.UTF-8"
LC_ALL=cs_CZ.UTF-8

并运行locale charmap返回:

UTF-8

关于Python-UnicodeEncodeError:“ charmap”编解码器无法编码字符pythonutf-8编码无法解码的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于nginx – uWSGI / Emperor:UnicodeEncodeError:’ascii’编解码器无法编码字符、Python - UnicodeDecodeError:'charmap' 编解码器无法解码位置 229393 中的字节 0x81:: 字符映射到 、Python 3 UnicodeDecodeError:“ charmap”编解码器无法解码字节0x9d、Python 3 UnicodeEncodeError:’ascii’编解码器无法编码字符的相关信息,请在本站寻找。

本文标签: