python后端写下载文件, 这个时候出现了这个错误

latin-1 codec cant encode characters in position 42-48: ordinal not in range256

怎么办:

查起因: 发现文件名有中文名字, 所以导致错误, 编码是latin-1编码, 所以我们需要解码成unicode在编码成latin-1

先看代码:

  这段代码涉及python转码问题, 一定要注意

                FilePathName = self.get_argument("FilePathName", None) #获取文件名
FileName = str(FilePathName.split("/")[-1]).strip() # 得到文件名
latinFileName = FileName.encode("utf-8").decode("latin1") # 这句很关键, 要解析成latin-1才OK,这样就不会报错
class DownFileHandler(downBaseRequestHandler):
@tornado.gen.coroutine
def get(self, *args, **kwargs): if self.verifyFlag == 1 and self.status == 0:
status = 2000 try:
FilePathName = self.get_argument("FilePathName", None)
FilePathName = MyBase64.decryption(unquote(FilePathName)) # 这句是解密
FileName = str(FilePathName.split("/")[-1]).strip()
latinFileName = FileName.encode("utf-8").decode("latin1") # 这句很关键, 要解析成latin-1才OK,这样就不会报错
newFileName = str(int(time.time())) + "." + FileName.split(".")[1] # 第二种方式就是换一个文件名
self.set_header("Content-Type", "application/x-zip-compressed")
# -----<或者这么写> ----- #
# self.set_header("Content-Type", "application/octet-stream")
self.set_header("Content-Disposition", "attachment; filename=%s" % latinFileName)
f = open(FilePathName, 'rb')
while True:
b = f.read(8096)
if not b:
break
else:
self.write(b)
self.flush()
f.close() except Exception as e:
e = FormatErrorCode(e)
my_log.error(e)
status = int(e[0])
self.write(json.dumps(result(status=status)))
self.finish()
else:
self.write(json.dumps(result(status=4005)))
self.finish()

latin-1 codec cant encode characters in position 42-48: ordinal not in range256 下载文件时候报错的更多相关文章

  1. Python编码问题:UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(12

    今天安装了PyScripter编辑器,刚要写代码,突然就出现异常: <span style="font-size:14px;color:#ff0000;">>&g ...

  2. python+selenium运行报错UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)

    使用python+selenium运行自动化脚本时,打印某一段文字出现UnicodeEncodeError: 'ascii' codec can't encode characters in posi ...

  3. web.py+mysql插入中文提示query = query.encode(charset) UnicodeEncodeError: 'latin-1' codec can't encode characters in position 86-100

    对于中文编码的问题,总会出现各种各样恶心的错误,还不知道应该怎么解决,首先,你从最开头就应该关注编码问题,尽量保证所有的编码方式都是一致的 用python+web.py+mysql来写程序,首先要保证 ...

  4. 'ascii' codec can't encode characters in position 0-8: ordinal not in range(128)的解决办法

    使用的python2.7,运行的时候出现了'ascii' codec can't encode characters in position 0-8: ordinal not in range(128 ...

  5. Pip 安装 出现UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-5: ordinal not in

    在Python 环境下,使用PiP 命令安装时,报错提示: UnicodeEncodeError: 'ascii' codec can't encode characters in position ...

  6. 解决UnicodeEncodeError: 'ascii' codec can't encode characters in position 问题(转)

    UnicodeEncodeError: 'ascii' codec can't encode characters in position 8-11: ordinal not in range(128 ...

  7. UnicodeEncodeError: 'ascii' codec can't encode characters in position 14-15: ordinal not in range(128)

    python在安装时,默认的编码是ascii,当程序中出现非ascii编码时,python的处理常常会报类似这样的错误. UnicodeEncodeError: 'ascii' codec can't ...

  8. 解决Python2.7的UnicodeEncodeError:'ascii' codec can't encode characters in position 0-78: ordinal not in range(128)异常错误

    解决Python2.7的UnicodeEncodeError: 'ascii' codec can't encode异常错误 大家都知道,在使用python进行网络爬虫时,最头疼的就是转码问题,下面是 ...

  9. 【转】Python3—UnicodeEncodeError 'ascii' codec can't encode characters in position 0-1

    转自:https://blog.csdn.net/AckClinkz/article/details/78538462 环境 >>> import sys >>> ...

随机推荐

  1. 【Unity】3.3 用3ds Max 2015制作模型并将其导入到Unity

    分类:Unity.C#.VS2015 创建日期:2016-04-05 一.常用三维软件简介 由于游戏引擎本身的建模功能相对较弱,无论是专业性还是自由度都无法同专业的三维软件相比,所以大多数游戏中的模型 ...

  2. hdu 1874 畅通工程续(求最短距离,dijkstra,floyd)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1874 /************************************************* ...

  3. 使用tar命令解压的时候报错not in gzip format

    使用tar命令解压一个xx.tar.gz压缩包的时候报错not in gzip format,后用file xx.tar.gz查看一下格式发现是html document text格式的...

  4. .NET MVC+ EF+LINQ 多表联查VIEW显示列表

    1.VIEW 页面显示代码 <link href="~/Content/bootstrap.css" rel="stylesheet" /> < ...

  5. Android 编程下 Canvas and Drawables

    Canvas and Drawables 安卓提供了一组绘制二维图形的 API(参考官方文档:Canvas and Drawables | Android Developers),这组 API 允许开 ...

  6. Kafka 协议实现中的内存优化【转】

    Kafka 协议实现中的内存优化   Jusfr 原创,转载请注明来自博客园 Request 与 Response 的响应格式 Request 与 Response 都是以 长度+内容 形式描述, 见 ...

  7. sql随机查询数据order by newid()

    方法1:最普通的写法,性能慢 ID,name FROM dt_keyword ORDER BY NEWID() 方法2:性能还可以 //先给数据库增加一列ALTER TABLE dt_keyword ...

  8. OpenSUSE安装软件

    zypper se xxxxx 是搜索软件包 zypper in xxxxx 跟你的apt-get install xxxx等价 zypper rm xxxx 删除 zypper up xxxx 更新 ...

  9. 通过python-libvirt管理KVM虚拟机 代码实现

    初步代码 <span style="font-size:18px;">''''' Work with virtual machines managed by libvi ...

  10. 第22章 RTX 低功耗之停机模式

    以下内容转载自安富莱电子: http://forum.armfly.com/forum.php STM32F103 停机模式介绍 本章节我们主要讲解停机模式,停机模式是在 Cortex™-M3 的深睡 ...