问题标签 [python-unicode]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
python - Django 管理员输入导致 UnicodeDecodeError,怎么办?
今天我通过 Django 管理员收到了无法编码的数据。不知何故,数据的编码不是 unicode。这怎么可能?
我的模型中有一个name
属性,Client
它以 unicode 格式返回数据:
但这不起作用:
足够奇怪,将名字/姓氏编码为常规字符串确实有效:
这里发生了什么?这个输入是如何通过管理员进入我的模型的?
系统堆栈(它是一个外部服务器):
- Debian 6.0.5(挤压)
- Django 1.4.1
- Python 2.6.6
- MySQL 5.1.49
- MySQL-python==1.2.2
这是相关的模型代码:
python - UnicodeDecodeError:“utf8”编解码器无法解码字节 0x9c
我有一个套接字服务器,它应该从客户端接收 UTF-8 有效字符。
问题是一些客户端(主要是黑客)正在通过它发送所有错误类型的数据。
我可以轻松区分真正的客户,但我将所有发送的数据记录到文件中,以便以后进行分析。
有时我会得到这样œ
的字符导致UnicodeDecodeError
错误。
我需要能够使用或不使用这些字符来制作字符串 UTF-8。
更新:
对于我的特殊情况,套接字服务是 MTA,因此我只希望接收 ASCII 命令,例如:
我在 JSON 中记录了所有这些。
然后一些没有好心的人决定发送各种垃圾。
这就是为什么对于我的具体情况,去掉非 ASCII 字符是完全可以的。
python - Python,转换 4 字节字符以避免 MySQL 错误“不正确的字符串值:”
我需要(在 Python 中)将 4 字节字符转换为其他字符。这是将其插入到我的 utf-8 mysql 数据库中而不会出现错误,例如:“Incorrect string value: '\xF0\x9F\x94\x8E' for column 'line' at row 1”
通过将 4 字节 unicode 插入 mysql 引发的警告显示以这种方式执行此操作:
但是,我在评论中遇到与用户相同的错误,“...错误的字符范围..”这显然是因为我的 Python 是 UCS-2(不是 UCS-4)构建。但是后来我不清楚该怎么做?
python - 如何在python中正确处理非ASCII字符串
我正在构建一个应用程序,该应用程序在数据库中包含带有拉丁符号的数据。用户可以输入这些数据。到目前为止,我一直在做的是在模板中显示数据时在最后对每个用户输入进行编码('latin2')和解码('latin2')。这有点烦人,我想知道是否有更好的方法来处理这个问题。
python - OpenERP code throws exceptions when run in background
I don't know if this should be posted under serverfault or stackoverflow. I don't know if it's a locale problem on my server or if it's python or the OpenERP application that is acting up.
I have a OpenERP system and I have encountered a quite strange problem.
In the openERP modules there are some "print"-lines that various developers used for debugging the application.
Like these:
and
If I start OpenERP in a ordinary terminal it will run just fine in the foreground and print the debug log and the prints in the console. The application never gives me an exception.
Now to the problem!
Earlier I've started the application in a screen so that I could detach it and close my connection to the server while the application is till running.
The code does not throw any exception when run in a 'screen'
Instead of a screen I want to have init.d script. I've made this script:
The script works, it start and stops the server.
When I use the module that imports data from a excel file I get this exception. I also get the exception in other places in the code where it tries to print a string containing the swedish letters 'åäö'.
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 3: ordinal not in range(128)
I thought it was something with the 'start-stop-daemon' command that changed the locale or somthing. So instead of forceing the application to the background with the argument '--background' I tried this:
This solved the UnicodeEncodeError. All joy I thought. But!
Now I get a Input/Output error
And the code that generates this
The 'pay_order' variable is a string that sometimes contain 'åäö'.
I can't understand why it behaves like this.
python - PIL:如何使用 draw.text 在图像上绘制€?
我有一个表单值的标题(“cadeau check 50 €”),我想用arial.ttf
. 我的文字是正确的,但对于欧元符号。我有 2 [] 到位。我不知道问题出在哪里。这是 PIL 中的编码问题,还是我的字体有问题?
python - Python DELETE 字符(Unicode 7F)似乎没有做任何事情。它不应该删除后续字符,还是我做错了?
我进入我的 Python shell 并对其进行了测试,结果如下:
根据我对这个字符的理解,它应该没有在那个字符串中显示空格。我究竟做错了什么?
file-upload - 用 mysql 和 python 转义字符混淆
我想从http://werkzeug.pocoo.org/上传徽标图片 保存后我尝试了以下操作。我是菜鸟所以请帮助..
当我通过 html 表单上传时收到的流是
我使用 req.file["name"] 收到上述内容并将其存储在 Mysql 中。
现在,当我从 MySQL 读回时,我得到了,
现在所有的转义字符都消失了,我想显示这个文件,但在我发送响应后我无法在 HTML 上显示。
为什么会发生这种情况,我该如何解决?我无法通过这个超过一个星期。
我正在为 python 使用 python2.7.3 和 werkzeug 和 mysql-connector。
其中一个 github 中的示例代码使用了这个
这完美地工作,它的所有正常文本..我怎么能得到这个。?
python - 在 python 中抓取页面时保留非拉丁字符
我有一个程序可以抓取一个页面,解析它的任何链接,然后下载链接到的页面(听起来像一个爬虫,但它不是)并将每个页面保存在一个单独的文件中。用于保存的文件名是页面 url 的一部分。例如,如果我找到指向 www.foobar.com/foo 的链接,我会下载该页面并将其保存在名为 foo.xml 的文件中。
稍后,我需要遍历所有此类文件并重新下载它们,使用文件名作为 url 的最后一部分。(所有页面都来自一个站点。)
它运行良好,直到我在 url 中遇到非拉丁字符。该站点使用utf-8,因此当我下载原始页面并对其进行解码时,它可以正常工作。但是当我尝试使用解码后的 url 下载相应的页面时,它不起作用,因为我认为编码是错误的。我尝试在文件名上使用 .encode() 将其改回,但它没有任何改变。
我知道这一定很简单,并且是我没有正确理解编码问题的结果,但我已经为此苦苦挣扎了很长时间。我已经多次阅读 Joel Spolsky 对编码的介绍,但我仍然无法完全确定在这里做什么。谁能帮我?
非常感谢,bsg
这是一些代码。我没有收到任何错误;但是当我尝试使用页面名称作为 url 的一部分下载页面时,我被告知该页面不存在。当然不是——没有像 abc/x54 这样的页面。
澄清一下:我下载了一个页面的 html,其中包含指向 www.foobar.com/Mehmet Kenan Dalbaşar 的链接,例如,但它显示为 Mehmet_Kenan_Dalba%C5%9Far。当我尝试下载页面 www.foobar.com/Mehmet_Kenan_Dalba%C5%9Far 时,页面是空白的。如何保留 www.foobar.com/Mehmet Kenan Dalbaşar 并在需要时将其返回到站点?
python - How do I allow opening of files that have Unicode characters in their filenames?
I have this Python script here that opens a random video file in a directory when run:
Source: An answer in my Super User post, 'How do I open a random file in a folder, and set that only files with the specified filename extension(s) should be opened?'
This is called by a BAT file, with this as its script:
I put this BAT file in the directory I want to open random videos of.
In most cases it works fine. However, I can't make it open files with Unicode characters (like Japanese or Korean characters in my case) in their filenames.
This is the error message when the BAT file and Python script is run on a directory and opens a file with Unicode characters in its filename:
C:\TestDir>openrandomvideo.BAT
C:\TestDir>C:\Python27\python.exe "C:\Programs\Scripts\open-random-video.py" cd
The filename, directory name, or volume label syntax is incorrect.
Note that the filename of the .FLV video file in that log is changed from its original filename (소시.flv) to '∩╗┐' in the command line log.
EDIT: I learned that the above command line error message is due to saving the BAT file as 'UTF-8 with BOM'. Saving it as 'ANSI or UTF-16' shows the following message instead, but still does not open the file:
Now, the filename of the .FLV video file in that log is changed from its original filename (소시.flv) to '??.flv.' in the command line log.
I'm using Python 2.7 on Windows 7, 64-bit.
How do I allow opening of files that have Unicode characters in their filenames?