Picasa 相册标题编码。不是统一码?
我为 Google Picasa 服务编写了一个简单的客户端。我想要的是创建一个包含相册标题名称的文件夹,并将原始照片从服务下载到该文件夹。 如果标题中有任何非拉丁字符,我会收到 IOError:
IOError: [Errno 2] 没有这样的文件或目录: '\xd0\x9e\xd1\x81\xd0\xb5\xd0\xbd\xd1\x8c\Autumnal-Equinox.jpg'
代码示例:
import gdata.photos.service
import gdata.media
import os
import urllib2
gd_client = gdata.photos.service.PhotosService()
username = 'cha.com.ua'
albums = gd_client.GetUserFeed(user=username)
for album in albums.entry:
photos = gd_client.GetFeed(
'/data/feed/api/user/%s/albumid/%s?kind=photo' % (
username, album.gphoto_id.text))
for photo in photos.entry:
destination = os.path.join(album.title.text, photo.title.text)
out = open(destination, 'wb')
out.write(urllib2.urlopen(photo.content.src).read())
out.close()
我尝试使用 .decode('utf-8') 解码标题
,它不起作用。
I wrote a simple client for Googles Picasa service. What I want is to create a folder with albums title name and download original photo from the service to this folder.
If there is any non-latin characters in title I got an IOError:
IOError: [Errno 2] No such file or directory:
'\xd0\x9e\xd1\x81\xd0\xb5\xd0\xbd\xd1\x8c\Autumnal-Equinox.jpg'
Code sample:
import gdata.photos.service
import gdata.media
import os
import urllib2
gd_client = gdata.photos.service.PhotosService()
username = 'cha.com.ua'
albums = gd_client.GetUserFeed(user=username)
for album in albums.entry:
photos = gd_client.GetFeed(
'/data/feed/api/user/%s/albumid/%s?kind=photo' % (
username, album.gphoto_id.text))
for photo in photos.entry:
destination = os.path.join(album.title.text, photo.title.text)
out = open(destination, 'wb')
out.write(urllib2.urlopen(photo.content.src).read())
out.close()
I tried to decode the title with .decode('utf-8')
, it does't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你说:
这
不可能是真的。如果第一个语句正确,第二个语句将导致:
看来您的
str
对象是一个 UTF-8 编码的西里尔字符串:而且上面的内容与错误消息中的文本完全不同: '\xd0 \x9e\xd1\x81\xd0\xb5\xd0\xbd\xd1\x8c\Autumnal-Equinox.jpg'
REVERSE SOLIDUS(反斜杠)表示您正在 Windows 上运行。 Windows 就是不理解 UTF-8。输入时将所有文本转换为 Unicode。对所有路径和文件名使用 Unicode。有效的简单示例:
You say:
and
This cannot be true. If the first statement is correct, the second will cause:
It appears that your
str
object is a UTF-8 encoded Cyrillic string:Also the above is quite unlike the text in the error message: '\xd0\x9e\xd1\x81\xd0\xb5\xd0\xbd\xd1\x8c\Autumnal-Equinox.jpg'
The REVERSE SOLIDUS (backslash) indicates that you are running on Windows. Windows just doesn't grok UTF-8. Convert all your text to Unicode on input. Use Unicode for all paths and filenames. Simple example which works: