python中文字符串编码_python,_python中文字符串编码的应用?,python - phpStudy

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

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

目录结构如下

相关阅读:

手机网页,如何让输入框被点击获取焦点时,不调用输入法?

gulp的save-dev参数表示什么意思?

使用 phonegap 构建应用出现的问题

请教一个数据库设计表问题

入门Redisredis set 数据不一致问题

JavaScript怎么判断一个字母小写大写?

一段c++代码编译通过了,但是没有输出

json数据中含有array返回类型的怎么解析?

有关 a herf 触发的问题- modal 插件

myisam和innodb哪个用的多

有一个apache的配置如下,请解释每一行的含义

php中遍历数组查找数组元素是否存在与当前字符串,如果不存在就输出当前字符串,如果存在,则输出数组的键值

怎样实现点击上传按钮预览图片的功能?

ionic 项目 ionic build android -release 打包时报错

一个简单的两面翻转效果,backface-visibility: hidden;无效怎么回事?

如何更好地写压缩程序?

codeblocks 删除一行快捷键有吗?

用sonar分析项目后单元测试覆盖率结果为空。

初学JS,想要制作一个“点击链接,在当前页面显示,不发生跳转”的平稳退化demo,遇到了一些问题,希望得到大家的帮助!

c# Newtonsoft.Json 有类似Java Jackson 里 JsonNode的对像吗

你可能感兴趣的:(python中文字符串编码)