关于 Python3 字符串比较问题

2016-07-10 15:44:16 +08:00
 cosven

我有两个问题,还请大家帮助解答:

  1. 为什么 a != b
  2. 既然 a != b,为啥后面检查路径的时候 os.path.exists(current_path, b)True
In [33]: os.listdir('.')
Out[33]:
['.DS_Store',
 'Closer - Travis.mp3',
 'I Really Like You - Carly Rae Jepsen.mp3',
 'Love Story - Taylor Swift.mp3',
 'Per dimenticare - Zero Assoluto.mp3',
 "Sono Bugiarda (I'm A Believer) - Caterina Caselli.mp3",
 'The Phoenix - Fall Out Boy.mp3',
 'We Are Never Ever Getting Back Together - Taylor Swift.mp3',
 'You Got Me - Colbie Caillat.mp3',
 'ガーネット - 奥華子.mp3',
 'ブルーバード - いきものがかり.mp3',
 '初恋 - 奥華子.mp3',
 '思念是一种病(Live) - live - 张震岳.mp3',
 '最后一班车 - 刺猬.mp3',
 '梁山伯与祝英台 - 群星.mp3']

In [34]: a = os.listdir('.')[-5]

In [35]: a
Out[35]: 'ブルーバード - いきものがかり.mp3'

In [36]: b = 'ブルーバード - いきものがかり.mp3'  # 用鼠标复制这个字符串

In [37]: a == b   # 这里很奇怪,明明是这样复制下来的,却不相等
Out[37]: False

In [38]: current_path = os.getcwd()

In [39]: b_path = os.path.join(current_path, b)

In [40]: os.path.exists(b_path)  # 这里也很奇怪,既然上面 a 和 b 不相等,这里路径又存在。搞不懂?
Out[40]: True

链接: https://gist.github.com/cosven/9e11707a8ebe98bc95948167a0001449

1663 次点击
所在节点    问与答
1 条回复
mimzy
2016-07-10 23:06:28 +08:00
>>> a
'ブルーバード - いきものがかり.mp3'
>>> b
'ブルーバード - いきものがかり.mp3'

>>> a.encode('unicode_escape')
b'\\u30d5\\u3099\\u30eb\\u30fc\\u30cf\\u3099\\u30fc\\u30c8\\u3099 - \\u3044\\u304d\\u3082\\u306e\\u304b\\u3099\\u304b\\u308a.mp3'
>>> b.encode('unicode_escape')
b'\\u30d6\\u30eb\\u30fc\\u30d0\\u30fc\\u30c9 - \\u3044\\u304d\\u3082\\u306e\\u304c\\u304b\\u308a.mp3'

注意看第一个字「ブ」, a 中 2 个字节, b 中 1 个:
フ U+30D5 http://unicode-table.com/cn/30D5/


(此处手动换行防组合) U+3099 http://unicode-table.com/cn/3099/

ブ U+30D6 http://unicode-table.com/cn/30D6/

a 中的「ブ バ ド が」都是「平假名 + 浊音符」组成的,根据 http://www.cnblogs.com/jessonluo/p/4801580.html 上的资料显示:「 Unicode 中重音符号有两种表示方法,用一个字节表示,或者用基字母加上重音符号表示,在 Unicode 中他们是相等的,但是在 Python 中由于通过 code point 来比较大小,所以就不相等了。」

可以通过使用 unicodedata.normalize 选择合适的方法去比较,类似于这种 http://r9.hateblo.jp/entry/2015/05/11/233000

完全不懂日文,以上都是搜索找到的~

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/291507

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX