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()

clipboard.png

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

clipboard.png

回答:

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

回答:

import sys
reload(sys)
sys.setdefaultencoding('utf8')

以上是 python中文字符串编码的应用? 的全部内容, 来源链接: utcz.com/a/156988.html

回到顶部