【问题标题】:Python: handling non-Ascii characters in a Google Picasa feedPython:处理 Google Picasa 提要中的非 Ascii 字符
【发布时间】:2015-09-03 10:58:26
【问题描述】:

我正在编写一个 Python 脚本,以将 Google 照片/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

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

【问题讨论】:

    标签: python unicode utf-8 picasa


    【解决方案1】:

    我想通了。问题出在 Mac 端而不是 Google 端。这是解决问题的更改后的代码行:

    ...
    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?

    【讨论】:

      猜你喜欢
      • 2013-03-22
      • 1970-01-01
      • 2010-12-11
      • 2013-07-13
      • 2015-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多