Python:在googlepicasa提要中处理nonAscii字符

2024-03-29 15:11:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个Python脚本,将Google Photos/Picasa与本地文件夹(在Mac上)同步。Picasa上的每个相册=一个本地文件夹。你知道吗

我的一些相册标题中包含非Ascii字符,我无法正确地将这些相册标题与本地文件夹名称进行比较。找不到匹配项,因此总是删除本地文件夹,然后重新创建。有人能帮我解决这个问题吗?你知道吗

下面的代码显示了我当前的脚本,包括我尝试将专辑标题和文件夹名称转换为unicode,我认为这就是答案:

import os
import gdata.photos.service

username = 'mattbarr99'
target = '/Users/home/Desktop/GooglePhotos/'

gd_client = gdata.photos.service.PhotosService()

# get Google album titles
google_albums = gd_client.GetUserFeed(user=username)
google_album_titles = [unicode(album.title.text, 'utf-8') for album in google_albums.entry]

# check local folders
for entry in os.listdir(target):
    if not os.path.isdir(os.path.join(target, entry)):
        continue
    if unicode(entry, 'utf-8') not in google_album_titles:
        print "removing local folder: %s" % unicode(entry, 'utf-8')
        os.rmdir(os.path.join(target, entry))

# check Google albums
for album in google_albums.entry:
    local_album_path = os.path.join(target, album.title.text)
    if not os.path.exists(local_album_path):
        print "creating local folder: %s" % local_album_path
        os.mkdir(local_album_path)

运行脚本时看到的打印输出总是:

removing local folder: Aspö 2015
creating local folder: /Users/home/Desktop/GooglePhotos/Aspö 2015

我假设问题是文本编码,我只是不知道正确的处理方法。我希望我已经提供了足够的信息:这是我关于堆栈溢出的第一篇文章。非常感谢您的帮助!你知道吗


Tags: pathin脚本文件夹标题targetalbumos
1条回答
网友
1楼 · 发布于 2024-03-29 15:11:13

我想出来了。问题出在Mac方面,而不是谷歌方面。以下是解决问题的更改代码行:

...
if unicodedata.normalize('NFC', unicode(entry, 'utf-8')) not in google_album_titles:
...

更多信息,请看我找到的堆栈溢出页面:

Unicode encoding for filesystem in Mac OS X not correct in Python?

相关问题 更多 >