Python findall、正则表达式、unicode
我正在尝试编写一个Python脚本,它通过目录树进行搜索,列出所有.flac文件,并从resp中派生Arist、Album和Title。dir/subdir/filename并将其写入文件。代码工作正常,直到遇到unicode字符为止。代码如下:Python findall、正则表达式、unicode,python,regex,unicode,findall,Python,Regex,Unicode,Findall,我正在尝试编写一个Python脚本,它通过目录树进行搜索,列出所有.flac文件,并从resp中派生Arist、Album和Title。dir/subdir/filename并将其写入文件。代码工作正常,直到遇到unicode字符为止。代码如下: import os, glob, re def scandirs(path): for currentFile in glob.glob(os.path.join(path, '*')): if os.path.isdir(curre
import os, glob, re
def scandirs(path):
for currentFile in glob.glob(os.path.join(path, '*')):
if os.path.isdir(currentFile):
scandirs(currentFile)
if os.path.splitext(currentFile)[1] == ".flac":
rpath = os.path.relpath(currentFile)
print "**DEBUG** rpath =", rpath
title = os.path.basename(currentFile)
title = re.findall(u'\d\d\s(.*).flac', title, re.U)
title = title[0].decode("utf8")
print "**DEBUG** title =", title
fpath = os.path.split(os.path.dirname(currentFile))
artist = fpath[0][2:]
print "**DEBUG** artist =", artist
album = fpath[1]
print "**DEBUG** album =", album
out = "%s | %s | %s | %s\n" % (rpath, artist, album, title)
flist = open('filelist.tmp', 'a')
flist.write(out)
flist.close()
scandirs('./')
代码输出:
**DEBUG** rpath = Thriftworks/Fader/Thriftworks - Fader - 01 180°.flac
**DEBUG** title = 180°
**DEBUG** artist = Thriftworks
**DEBUG** album = Fader
Traceback (most recent call last):
File "decflac.py", line 25, in <module>
scandirs('./')
File "decflac.py", line 7, in scandirs
scandirs(currentFile)
File "decflac.py", line 7, in scandirs
scandirs(currentFile)
File "decflac.py", line 20, in scandirs
out = "%s | %s | %s | %s\n" % (rpath, artist, album, title)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 46: ordinal not in range(128)
所以,我的问题是:
1) 为什么相同的代码可以在控制台中工作,而不能在脚本中工作?
2) 如何修复脚本?Python控制台与终端配合使用,并根据其区域设置解释unicode编码 将该行替换为新的
str.format
:
out = u"{} | {} | {} | {}\n".format(rpath, artist, album, title)
并在写入文件时编码为utf8:
with open('filelist.tmp', 'a') as f:
f.write(out.encode('utf8'))
或导入编解码器
并直接执行以下操作:
with codecs.open('filelist.tmp', 'a', encoding='utf8') as f:
f.write(out)
或者,由于utf8是默认值:
with open('filelist.tmp', 'a') as f:
f.write(out)
u
使其成为unicode字符串会有所帮助通过切换到Python3解决问题,Python3按预期处理unicode大小写。
替代:
title = title[0].decode("utf8")
用于:
title = title[0]
甚至不需要在“out”的值前面加上“u”或在写入时指定编码。我喜欢Python3。当对包含Unicode字符的文件名使用
glob
时,请使用Unicode字符串作为模式。这使得glob
返回Unicode字符串而不是字节字符串。输出时,打印Unicode字符串会自动在控制台的编码中对其进行编码。如果您的歌曲包含控制台编码不支持的字符,您仍然会遇到问题。在这种情况下,将数据写入UTF-8编码的文件,并在支持UTF-8的编辑器中查看
>>> import glob
>>> for f in glob.glob('*'): print f
...
ThriftworksFaderThriftworks - Fader - 01 180░.flac
>>> for f in glob.glob(u'*'): print f
...
ThriftworksFaderThriftworks - Fader - 01 180°.flac
这也适用于os.walk
,是执行递归搜索的一种更简单的方法:
#!python2
import os, fnmatch
def scandirs(path):
for path,dirs,files in os.walk(path):
for f in files:
if fnmatch.fnmatch(f,u'*.flac'):
album,artist,tracktitle = f.split(u' - ')
print 'Album: ',album
print 'Artist:',artist
title,track = tracktitle.split(u' ',1)
track = track[:-5]
print 'Track: ',track
print 'Title: ',title
scandirs(u'.')
输出:
Album: ThriftworksFaderThriftworks
Artist: Fader
Track: 180°
Title: 01
感谢您对控制台和区域设置的回复和解释。不幸的是,提议的代码修复似乎不起作用;在“out”的值前面加上“u”时,脚本停止,并出现相同的错误。我唯一能让它通过'out='的时候是在注释'title=title[0]。解码(“utf8”)'行时,而不是在'out'前面加上'u'。但随后脚本在write语句中退出;同样的错误。*我尝试了所有三种建议的write语句,感谢您对控制台和解释器之间差异的解释。完全有道理。不幸的是,建议的u前缀不起作用,请参阅我的回复eumiro的帖子。谢谢你,马克。仍然无法使用u作为glob的前缀,但是使用os.walk而不是glob构造,该脚本在unicode和Python2中运行良好。
Album: ThriftworksFaderThriftworks
Artist: Fader
Track: 180°
Title: 01