简体   繁体   English

Python,为什么使用 unicode 上传文件时会引发 UnicodeEncodeError?

[英]Python, why UnicodeEncodeError raised when uploading files with unicode?

I am starting to learning python and I just cant.我开始学习 python 而我就是学不会。 I have simple server on flask, and I get error on my prod environment, but, all works ok on dev.我在 flask 上有一个简单的服务器,我的 prod 环境出现错误,但是,在 dev 上一切正常。

: file.save('/tmp/' +str( file.filename))
  File "/home/*****/*********/backend/lib64/python3.6/site-

 packages/werkzeug/datastructures.py", line 3066, in save   dst =
 open(dst, "wb")   UnicodeEncodeError: 'ascii' codec can't encode
 characters in position 5-12: ordinal not in range(128)

I get this error when upload files with unicode character in filename.上传文件名中包含 unicode 字符的文件时出现此错误。 On my dev and prod environments I have similar python, 3.7, and similar LANG variable, ru_RU.UTF8 sys.getdefaultencoding() return 'utf8' export PYTHONIOENCODING=utf-8 didnt resolve the error.在我的开发和生产环境中,我有类似的 python、3.7 和类似的 LANG 变量 ru_RU.UTF8 sys.getdefaultencoding() return 'utf8' export PYTHONIOENCODING=utf-8 没有解决错误。 I just dont understand what should I do.我只是不明白我该怎么做。

UPDATE: looks like environment variable solve the problem when I start python process from console, BUT I still get error when i run python process from systemd.更新:当我从控制台启动 python 进程时,看起来环境变量解决了问题,但是当我从 systemd 运行 python 进程时我仍然收到错误。 Command completly the same, {PATH_TO_PYTHON} {PY_FILE}, but I get the encoding error in random places when run as systemd.命令完全相同,{PATH_TO_PYTHON} {PY_FILE},但是当以 systemd 运行时,我在随机位置出现编码错误。 Please help.请帮忙。

Update: just checked: python process work fine from console, but fail as systemd.更新:刚刚检查:python 进程从控制台工作正常,但作为 systemd 失败。 Systemd file:系统文件:

[Service]
 Environment=PGCLIENTENCODING=UTF8
Environment=PYTHONIOENCODING=utf8
 
ExecStart=/home/*****/backend/bin/python /home/*****/backend/helloflask.py
WorkingDirectory=/home/*********/backend

My env output我的环境 output

XDG_SESSION_ID=734
 SHELL=/bin/bash
TERM=xterm-256color
HISTSIZE=1000
 SSH_TTY=/dev/pts/1
PYTHONIOENCODING=utf8
 LS_COLORS=rs=0:di=38;5;27:ln=38;5;51:mh=44;38;5;15:pi=40;38;5;11:so=38;5;13:do=38;5;5:bd=48;5;232;38;5;11:cd=48;5;232;38;5;3:or=48;5;232;38;5;9:mi=05;48;5;232;38;5;15:su=48;5;196;38;5;15:sg=48;5;11;38;5;16:ca=48;5;196;38;5;226:tw=48;5;10;38;5;16:ow=48;5;10;38;5;21:st=48;5;21;38;5;15:ex=38;5;34:*.tar=38;5;9:*.tgz=38;5;9:*.arc=38;5;9:*.arj=38;5;9:*.taz=38;5;9:*.lha=38;5;9:*.lz4=38;5;9:*.lzh=38;5;9:*.lzma=38;5;9:*.tlz=38;5;9:*.txz=38;5;9:*.tzo=38;5;9:*.t7z=38;5;9:*.zip=38;5;9:*.z=38;5;9:*.Z=38;5;9:*.dz=38;5;9:*.gz=38;5;9:*.lrz=38;5;9:*.lz=38;5;9:*.lzo=38;5;9:*.xz=38;5;9:*.bz2=38;5;9:*.bz=38;5;9:*.tbz=38;5;9:*.tbz2=38;5;9:*.tz=38;5;9:*.deb=38;5;9:*.rpm=38;5;9:*.jar=38;5;9:*.war=38;5;9:*.ear=38;5;9:*.sar=38;5;9:*.rar=38;5;9:*.alz=38;5;9:*.ace=38;5;9:*.zoo=38;5;9:*.cpio=38;5;9:*.7z=38;5;9:*.rz=38;5;9:*.cab=38;5;9:*.jpg=38;5;13:*.jpeg=38;5;13:*.gif=38;5;13:*.bmp=38;5;13:*.pbm=38;5;13:*.pgm=38;5;13:*.ppm=38;5;13:*.tga=38;5;13:*.xbm=38;5;13:*.xpm=38;5;13:*.tif=38;5;13:*.tiff=38;5;13:*.png=38;5;13:*.svg=38;5;13:*.svgz=38;5;13:*.mng=38;5;13:*.pcx=38;5;13:*.mov=38;5;13:*.mpg=38;5;13:*.mpeg=38;5;13:*.m2v=38;5;13:*.mkv=38;5;13:*.webm=38;5;13:*.ogm=38;5;13:*.mp4=38;5;13:*.m4v=38;5;13:*.mp4v=38;5;13:*.vob=38;5;13:*.qt=38;5;13:*.nuv=38;5;13:*.wmv=38;5;13:*.asf=38;5;13:*.rm=38;5;13:*.rmvb=38;5;13:*.flc=38;5;13:*.avi=38;5;13:*.fli=38;5;13:*.flv=38;5;13:*.gl=38;5;13:*.dl=38;5;13:*.xcf=38;5;13:*.xwd=38;5;13:*.yuv=38;5;13:*.cgm=38;5;13:*.emf=38;5;13:*.axv=38;5;13:*.anx=38;5;13:*.ogv=38;5;13:*.ogx=38;5;13:*.aac=38;5;45:*.au=38;5;45:*.flac=38;5;45:*.mid=38;5;45:*.midi=38;5;45:*.mka=38;5;45:*.mp3=38;5;45:*.mpc=38;5;45:*.ogg=38;5;45:*.ra=38;5;45:*.wav=38;5;45:*.axa=38;5;45:*.oga=38;5;45:*.spx=38;5;45:*.xspf=38;5;45:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
MAIL=/var/spool/mail/root
PWD=/root
LANG=ru_RU.UTF-8
PGCLIENTENCODING=UTF8
HISTCONTROL=ignoredups
 SHLVL=2
 PYTHON_ENV=prod
LESSOPEN=||/usr/bin/lesspipe.sh %s
XDG_RUNTIME_DIR=/run/user/0
_=/usr/bin/env

I found a solution right after posting this question, obliviosly.我在发布这个问题后立即找到了解决方案,忘记了。 export PYTHONIOENCODING=utf-8 do the right things, I get same error, but when trying to update database. export PYTHONIOENCODING=utf-8 做正确的事情,我得到同样的错误,但是在尝试更新数据库时。 THe solution was export PGCLIENTENCODING=UTF8解决方案是 export PGCLIENTENCODING=UTF8

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

相关问题 为什么UnicodeEncodeError只在emacs的python shell中引发? - Why UnicodeEncodeError raised only in emacs's python shell? Python Unicode UnicodeEncodeError - Python Unicode UnicodeEncodeError 调用'createsuperuser'时引发UnicodeEncodeError - UnicodeEncodeError raised when 'createsuperuser' is called 尝试上传具有unicode内容的XML时,Python ftplib UnicodeEncodeError - Python ftplib UnicodeEncodeError when trying to upload an XML with unicode content python,UnicodeEncodeError,将unicode转换为ascii - python, UnicodeEncodeError, converting unicode to ascii 将python2与fromfrom __future__ import unicode_literals一起使用时,python UnicodeEncodeError - python UnicodeEncodeError when use python2 with `from __future__ import unicode_literals` 混合整数和unicode时的另一个UnicodeEncodeError - Another UnicodeEncodeError when mixing integers and unicode __str__已经返回unicode时出现UnicodeEncodeError - UnicodeEncodeError when __str__ already returns unicode 在 JSON 中接收 emoji unicode 时出现 UnicodeEncodeError - UnicodeEncodeError when receiving emoji unicode in JSON 能够在Eclipse中使用Unicode字符串运行Python代码,但在通过命令行或空闲运行时获取UnicodeEncodeError。 - Able to run Python code with Unicode string in Eclipse, but getting UnicodeEncodeError when running via command line or Idle.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM