ccw*_*te1 0 python string mp3 id3
我有一个字符串,它是我从MP3 ID3标签获得的艺术家的名字
sArtist = "The Beatles"
Run Code Online (Sandbox Code Playgroud)
我想要的是改变它
sArtist = "Beatles, the"
Run Code Online (Sandbox Code Playgroud)
我遇到了两个不同的问题.我的第一个问题是我似乎在为''换取''.
if sArtist.lower().find('the') == 0:
sArtist = sArtist.lower().replace('the','')
sArtist = sArtist + ", the"
Run Code Online (Sandbox Code Playgroud)
我的第二个问题是因为我必须检查"The"和"the"我使用sArtist.lower().然而,这将我的结果从"甲壳虫乐队"改为"披头士乐队".为了解决这个问题,我刚刚删除了.lower并添加了第二行代码来明确查找这两种情况.
if sArtist.lower().find('the') == 0:
sArtist = sArtist.replace('the','')
sArtist = sArtist.replace('The','')
sArtist = sArtist + ", the"
Run Code Online (Sandbox Code Playgroud)
所以我真正需要解决的问题是为什么我用' <SPACE>而不是'代替'' <NULL>.但如果有人有更好的方法来做到这一点我会很高兴教育:)
运用
sArtist.replace('The','')
Run Code Online (Sandbox Code Playgroud)
很危险 如果艺术家的名字是西奥多,会发生什么?
也许使用正则表达式:
In [11]: import re
In [13]: re.sub(r'^(?i)(a|an|the) (.*)',r'\2, \1','The Beatles')
Out[13]: 'Beatles, The'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
253 次 |
| 最近记录: |