社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  问与答

又是字符问题

1160974245 • 9 年前 • 5707 次点击  

上传文件到bcs上,问题是上传文件名是中文和纯数字就报错,字母加数字没问题。 想不通。


中文:ascii' codec can't encode characters in position 9-11: ordinal not in range(128) 数字:'utf8' codec can't decode byte 0x9c in position 140: invalid start byte 谢谢!


def upfile(request):
    global BUCKET,bcs
    if request.method == 'GET':
        return render_to_response('upload.html')
    elif request.method == 'POST':
        file = request.FILES['cs']
        f    = handfile(file)
        b = bcs.bucket(BUCKET)
        name = '/%s' % file.name
        o = b.object(name)
        o.put_file(f)
        os.remove(f)
        return HttpResponseRedirect('/p/')
def handfile(f):
    fpath = '/tmp/%s' % f.name
    with open(fpath,'wb+') as info:
        for chunk in f.chunks():
            info.write(chunk)
    return fpath
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/847
 
5707 次点击  
文章 [ 9 ]  |  最新文章 9 年前
1160974245
Reply   •   1 楼
1160974245    9 年前

@9FHT51 是python2

9FHT51
Reply   •   2 楼
9FHT51    9 年前

是py2吗 py3从来都不会遇到编码问题呀

1160974245
Reply   •   3 楼
1160974245    9 年前

真的可以了,太感谢了!

涂伟忠
Reply   •   4 楼
涂伟忠    9 年前
#声明一个object
obj = bucket.object(remote)
# 上传
obj.put_file(local)

这里的 remote 和 local 都必须是 utf-8

if isinstance(local, unicode):
    local = local.encode('utf-8')

if isinstance(remote, unicode):
    remote = remote.encode('utf-8')

这样应该就行了

1160974245
Reply   •   5 楼
1160974245    9 年前

@Django中国社区 编辑器不会用 编辑的和下面预览的不一样

1160974245
Reply   •   6 楼
1160974245    9 年前

@Django中国社区 不行

Py站长
Reply   •   7 楼
Py站长    9 年前

http://in355hz.iteye.com/blog/1860787 这篇文章 看看

Py站长
Reply   •   8 楼
Py站长    9 年前

try

reload(sys)                      # reload 才能调用 setdefaultencoding 方法  
sys.setdefaultencoding('utf-8')  # 设置 'utf-8'
VA
Reply   •   9 楼
VA    9 年前

代码能排版一下么