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. 深入理解LInux内核-进程通信

    进程间通信的基本机制:1.管道和FIFO(命名管道):最适合在进程之间实现生产者/消费者的交互.进程A向管道写入数据,进程B从管道读出数据.2.信号量:内核信号量的用户态版本.3.消息:允许进程在预定 ...

  2. android:clipChildren 属性

    这个属性默认为true: 两个用法: 1.设置为false ,使用ViewPager 的时候能够 实现一屏上显示多个Item 2.动画能越界

  3. 菜鸟学Java(十二)——搭建一个完整的Java开发环境

    作为一个Java程序员,配置一个java开发环境是必备的技能,今天给广大菜鸟初学者补上一课.环境的配置,大概就分三个1,JDK 2,Tomcat(或者其他的)3,eclipse(或者myeclipse ...

  4. numRecordsIn 在哪里实现?

    /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreem ...

  5. bug ,improvements, features jira等信息

    https://issues.apache.org/jira/secure/ReleaseNote.jspa?version=12341764&projectId=12315522 https ...

  6. 乐观锁的一种实现方式—CAS

    线程安全 众所周知,Java是多线程的.但是,Java对多线程的支持其实是一把双刃剑.一旦涉及到多个线程操作共享资源的情况时,处理不好就可能产生线程安全问题.线程安全性可能是非常复杂的,在没有充足的同 ...

  7. nginx配置长连接

    http { keepalive_timeout 20; --长连接timeout keepalive_requests 8192; --每个连接最大请求数 } events { worker_con ...

  8. mysql++ Query

    mysqlpp:: Query类存储了Connection的指针,可以用它进行SQL语句的增删改查. 连上数据库后,使用mysqlpp::Connection::query()获取Query对象 Qu ...

  9. python(45)内置函数:os.system() 和 os.popen()

    os.system() 和 os.popen() 概述 os.popen() 方法用于从一个命令打开一个管道. 在Unix,Windows中有效 语法 popen()方法语法格式如下: os.pope ...

  10. hive的join查询

    hive的join查询 语法 join_table: table_reference [INNER] JOIN table_factor [join_condition] | table_refere ...