Skip to content

Conversation

@raxxar1024
Copy link
Contributor

No description provided.

@jemygraw
Copy link
Contributor

你可以在你的代码顶部放一行:

# -*- coding: utf-8 -*-

看看么

@raxxar1024
Copy link
Contributor Author

你好,代码头部是有utf-8的。

我写了个测试程序如下:

# -*- coding: utf-8 -*-
from qiniu import Auth, put_file

access_key = 'XXXXXXX'
secret_key = 'YYYYYYY'
bucket_name = 'ZZZ'

q = Auth(access_key, secret_key)
PostFile = u'测试测试.txt'

token = q.upload_token(bucket_name, PostFile)
FilePath = u'.\\测试测试.txt'

ret, info = put_file(token, PostFile, FilePath)

还是会遇到如下报错:

......
  File "C:\Python27\lib\site-packages\qiniu\auth.py", line 142, in upload_token
    scope = '{0}:{1}'.format(bucket, key)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)

sdk版本是7.1.4

@jemygraw
Copy link
Contributor

你的代码保存的编码是utf8的么,目前相同代码无法复现问题

@raxxar1024
Copy link
Contributor Author

是的哦
这是我用notepad++看的:
image

用chardet模块看的:

>>> import chardet
>>> f = open("qiniu_test.py","rb")
>>> data=f.read()
>>> print(chardet.detect(data))
{'confidence': 0.99, 'encoding': 'utf-8'}
>>>

我的执行情况是:

E:\tmp\code_snippet>python qiniu_test.py
Traceback (most recent call last):
  File "qiniu_test.py", line 12, in <module>
    token = q.upload_token(bucket_name, PostFile)
  File "C:\Python27\lib\site-packages\qiniu\auth.py", line 142, in upload_token
    scope = '{0}:{1}'.format(bucket, key)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordin
al not in range(128)

@Folyd
Copy link

Folyd commented Jun 30, 2017

我也碰到了这个编码问题,也是7.1.4版本

@jemygraw jemygraw merged commit 198aa1c into qiniu:master Jul 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants