Python:在Google Picasa提要中处理非Ascii字符

Python:在Google Picasa提要中处理非Ascii字符,python,unicode,utf-8,picasa,Python,Unicode,Utf 8,Picasa,我正在编写一个Python脚本,将Google Photos/Picasa与本地文件夹(在Mac上)同步。Picasa上的每个相册=一个本地文件夹 我的一些相册标题中包含非Ascii字符,我无法正确地将这些相册标题与本地文件夹名称进行比较。找不到匹配项,因此始终会删除本地文件夹,然后重新创建。谁能帮我解决这个问题 下面的代码显示了我当前的脚本,包括我尝试将专辑标题和文件夹名称转换为unicode,我认为这就是答案: import os import gdata.photos.service u

我正在编写一个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

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

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

...
if unicodedata.normalize('NFC', unicode(entry, 'utf-8')) not in google_album_titles:
...
关于更多信息,这里是我找到的堆栈溢出页面,它帮助我找到了它: