python中文字符串编码的应用?

查看:102
本文介绍了python中文字符串编码的应用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

@login_required_ajax
def get_file_op_url(request, repo_id):
    """Get file upload/update url for AJAX.
    """
    content_type = 'application/json; charset=utf-8'

    op_type = request.GET.get('op_type') # value can be 'upload', 'update', 'upload-blks', 'update-blks'
    if not op_type:
        err_msg = _(u'Argument missing')
        return HttpResponse(json.dumps({"error": err_msg}), status=400,
                            content_type=content_type)

    username = request.user.username
    url = ''
    #parent_path = request.GET.get('path')
    dir_name = request.GET.get('dir-structure')
    parent_path = request.GET.get('path')
    yy=''
    if dir_name and dir_name != '':
        xx = dir_name.encode('utf-8')
        p,f=os.path.split(xx)
        yy=p
        f=open('/root/Desktop/out.txt','a')
        print >>f,p
        f.close()


        #check_name = check_filename_with_rename(repo_id, parent_path,dir_name2)
        # TODO: what if parrent_dir does not exist?
        cmmts = get_commits(repo_id, 0, 1)
        latest_commit = cmmts[0] if cmmts else None
        if not latest_commit:
            return ''
        dirents = seafile_api.list_dir_by_commit_and_path(repo_id, latest_commit.id,parent_path.encode('utf-8'))
        zz=[];
        if yy:
            zz = yy.split('/')

        
        xx=[]
        for x in dirents:
            xx.append(x.obj_name)
        if zz[0] not in xx:
            seafile_api.post_dir(repo_id, parent_path.encode('utf-8'),zz[0], username)


        if len(zz)>1:
            jj = len(zz)
            kk = parent_path
            for j in range(1,jj):
                kk += zz[j-1]+'/'
                
                #kk = parent_path+zz[j-1]+'/'
                dirents2 = seafile_api.list_dir_by_commit_and_path(repo_id, latest_commit.id,kk)
                uu=[]
                for d in dirents2:
                    uu.append(d.obj_name.encode('utf-8'))
                
                if zz[j]: 
                    if zz[j] not in uu:
                        seafile_api.post_dir(repo_id,kk,zz[j],username)
            f=open('/root/Desktop/out2.txt','a')
            print >>f,yy
            f.close()
                


    f=open('/root/Desktop/out3.txt','a')
    print >>f,yy
    f.close()
    if check_repo_access_permission(repo_id, request.user) == 'rw':
        token = seafile_api.get_fileserver_access_token(repo_id, 'dummy',
                                                        op_type, username)
        url = gen_file_upload_url(token, op_type + '-aj')
    if yy and yy[-1] != '/':
        yy += '/'
    #yy2 = yy.encode('utf-8')

    return HttpResponse(json.dumps({"url": url,"path":yy}), content_type=content_type)
    
    

我加的代码如下

    
    
dir_name = request.GET.get('dir-structure')
parent_path = request.GET.get('path')
yy=''
if dir_name and dir_name != '':
    xx = dir_name.encode('utf-8')
    p,f=os.path.split(xx)
    yy=p

    cmmts = get_commits(repo_id, 0, 1)
    latest_commit = cmmts[0] if cmmts else None
    if not latest_commit:
        return ''
    dirents = seafile_api.list_dir_by_commit_and_path(repo_id, latest_commit.id,parent_path.encode('utf-8'))
    zz=[];
    if yy:
        zz = yy.split('/')

    
    xx=[]
    for x in dirents:
        xx.append(x.obj_name)
    if zz[0] not in xx:
        seafile_api.post_dir(repo_id, parent_path.encode('utf-8'),zz[0], username)


    if len(zz)>1:
        jj = len(zz)
        kk = parent_path
        for j in range(1,jj):
            kk += zz[j-1]+'/'
            
            #kk = parent_path+zz[j-1]+'/'
            dirents2 = seafile_api.list_dir_by_commit_and_path(repo_id, latest_commit.id,kk)
            uu=[]
            for d in dirents2:
                uu.append(d.obj_name.encode('utf-8'))
            
            if zz[j]: 
                if zz[j] not in uu:
                    seafile_api.post_dir(repo_id,kk,zz[j],username)

    

我做的是文件夹上传,当我的目录为英文的时候没有问题
但是当我的目录名字为中文的时候就出现问题,
程序卡在

    if zz[0] not in xx:
        seafile_api.post_dir(repo_id, parent_path.encode('utf-8'),zz[0], username)
        

这里了
这段代码前后打印yy,后面yy比前面yy的值要少
是不是中文字符串的问题,not in 这里没有问题吧

中文名字的文件夹,我打印传过来的文件夹名(encode utf-8之后)显示的是正常的中文。

    
    
            try:
        if len(zz)>1:
            jj = len(zz)
            kk = parent_path
            for j in range(1,jj):
                kk += zz[j-1]+'/'
                
                #kk = parent_path+zz[j-1]+'/'
                dirents2 = seafile_api.list_dir_by_commit_and_path(repo_id, latest_commit.id,kk)
                uu=[]
                for d in dirents2:
                    uu.append(d.obj_name.encode('utf-8'))
                
                if zz[j]: 
                    if zz[j] not in uu:
                        seafile_api.post_dir(repo_id,kk,zz[j],username)
    except Exception,e:
        f=open('/root/Desktop/out2.txt','a')
        print >>f,e
        f.close()
        

因为我上传的文件有多层目录,第一层目录中的文件上传成功,内层没有完全上传成功。
目录结构如下

解决方案

你这里编码转换用的不对
参考一下:http://xanderzhang.iteye.com/blog/465992

这篇关于python中文字符串编码的应用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆