Python不读日文字符

时间:2018-05-18 22:29:42

标签: python character-encoding

我有一个脚本可以读取文件夹中的所有mp3文件,获取所有标签并将其排列为json对象。我正在使用id3-py-1.2库来获取标签。

它非常有效。除了阅读日文歌曲外,我找不到让它正确阅读日文字符的方法。

Here is my code:

#!/usr/bin/env python

import os
import json
from ID3 import *

extension = '.mp3'
result = []

def getFiles():
    for path, subdirs, files in os.walk(r'.'):
       for filename in files:
            f = os.path.join(path, filename)[2:]
            f = f.replace('\\', '/')
            if f[-4:] == extension:
                result.append(f)
    return result



files = getFiles()


def arrangeSong(files):
    songs = []

    for file in files:
        data = ID3(file)
        song = {}
        song['path'] = file
        song['artist'] = data.get('ARTIST', '')
        song['title'] = data.get('TITLE', '')
        song['album'] = data.get('ALBUM', '')
        song['trackNumber'] = data.get('TRACKNUMBER', '')
        songs.append(song)
    return songs

print "Content-type: text/html"
print
print json.dumps(arrangeSong(files), encoding="ISO-8859-1")

当它尝试读取文件名中包含日文字符的文件时,它会给我这个错误:

IOError: [Errno 22] invalid mode ('rb') or filename: '0.8 Byou to Shougeki - Hakai Pop/02 - ?????.mp3'

当文件“0.8 Byou to Shougeki - Hakai Pop \ 02 - 虹色の言葉.mp3”出现时停止

我试图将os.walk更改为:

for path, subdirs, files in os.walk(ur'.'):
       for filename in files:
            f = os.path.join(path, filename)[2:]
            f = f.replace('\\', '/')
            if f[-4:] == extension:
                result.append(f)
    return result

然后它成功读取文件,但仍然无法正确识别日文字符,给我这个结果带问号而不是字符:

[{"album": "??POP", "path": "0.8 Byou to Shougeki - Hakai Pop/01 - The Killing M
oon.mp3", "title": "The Killing Moon", "trackNumber": "", "artist": "0.8?????"},
 {"album": "??POP", "path": "0.8 Byou to Shougeki - Hakai Pop/02 - \u8679\u8272\
u306e\u8a00\u8449.mp3", "title": "?????", "trackNumber": "", "artist": "0.8?????
"}, {"album": "??POP", "path": "0.8 Byou to Shougeki - Hakai Pop/03 - \u6628\u65
e5\u3088\u308a\u82e5\u304f.mp3", "title": "??????", "trackNumber": "", "artist":
 "0.8?????"}, {"album": "??POP", "path": "0.8 Byou to Shougeki - Hakai Pop/04 -
\u767d\u663c\u5922.mp3", "title": "???", "trackNumber": "", "artist": "0.8?????"
}, {"album": "??POP", "path": "0.8 Byou to Shougeki - Hakai Pop/05 - KAGEROU.mp3
", "title": "KAGEROU", "trackNumber": "", "artist": "0.8?????"}, {"album": "??PO
P", "path": "0.8 Byou to Shougeki - Hakai Pop/06 - J.M.\u306f\u592a\u967d.mp3",
"title": "J.M.???", "trackNumber": "", "artist": "0.8?????"}, {"album": "??POP",
 "path": "0.8 Byou to Shougeki - Hakai Pop/07 - Fuck & Loud.mp3", "title": "Fuck
 & Loud", "trackNumber": "", "artist": "0.8?????"}, {"album": "??POP", "path": "
0.8 Byou to Shougeki - Hakai Pop/08 - \u5343\u306e\u30ca\u30a4\u30d5.mp3", "titl
e": "?????", "trackNumber": "", "artist": "0.8?????"}, {"album": "??POP", "path"
: "0.8 Byou to Shougeki - Hakai Pop/09 - \u91ce\u83dc\u304f\u3093\u4f53\u64cd201
5.mp3", "title": "??????2015", "trackNumber": "", "artist": "0.8?????"}, {"album
": "??POP", "path": "0.8 Byou to Shougeki - Hakai Pop/10 - \u30b8\u30e3\u30b9\u3
0df\u30f3\u306e\u604b\u4eba.mp3", "title": "????????", "trackNumber": "", "artis
t": "0.8?????"}, {"album": "??????????", "path": "0.8 Byou to Shougeki - Zarathu
stra/01 - \u30d6\u30ec\u30a4\u30af\u30d3\u30fc\u30c4\u306f\u5973\u795e\u306e\u30
5f\u3081\u306b.mp3", "title": "??????????????", "trackNumber": "", "artist": "0.
8?????"}, {"album": "??????????", "path": "0.8 Byou to Shougeki - Zarathustra/02
 - \u72c2\u97f3\u30df\u30af.mp3", "title": "????", "trackNumber": "", "artist":
"0.8?????"}, {"album": "??????????", "path": "0.8 Byou to Shougeki - Zarathustra
/03 - \u30ec\u30dc\u30ea\u30e5\u3002.mp3", "title": "?????", "trackNumber": "",
"artist": "0.8?????"}, {"album": "??????????", "path": "0.8 Byou to Shougeki - Z
arathustra/04 - \u9945\u982d\u3053\u308f\u3044.mp3", "title": "?????", "trackNum
ber": "", "artist": "0.8?????"}, {"album": "??????????", "path": "0.8 Byou to Sh
ougeki - Zarathustra/05 - \u75db\u307f\u306e\u72ac.mp3", "title": "????", "track
Number": "", "artist": "0.8?????"}, {"album": "?????????????EP", "path": "0.8\u7
9d2\u3068\u885d\u6483\u3002 - \u3044\u306a\u308a\u5bff\u53f8\u30ac\u30fc\u30eb\u
306e\u6d99\u3001\u3001\u3001EP/01 - ARISHIMA MACHINE GUN.mp3", "title": "ARISHIM
A MACHINE GUN///", "trackNumber": "1", "artist": "0.8?????"}, {"album": "???????
??????EP", "path": "0.8\u79d2\u3068\u885d\u6483\u3002 - \u3044\u306a\u308a\u5bff
\u53f8\u30ac\u30fc\u30eb\u306e\u6d99\u3001\u3001\u3001EP/02 - \u5e38\u76e4\u53f0
 \u30c8\u30e9\u30f3\u30b9\u76c6\u8e0a\u308a.mp3", "title": "??? ???????", "track
Number": "2", "artist": "0.8?????"}, {"album": "?????????????EP", "path": "0.8\u
79d2\u3068\u885d\u6483\u3002 - \u3044\u306a\u308a\u5bff\u53f8\u30ac\u30fc\u30eb\
u306e\u6d99\u3001\u3001\u3001EP/03 - KAGEROU.mp3", "title": "KAGEROU", "trackNum
ber": "3", "artist": "0.8?????"}, {"album": "?????????????EP", "path": "0.8\u79d
2\u3068\u885d\u6483\u3002 - \u3044\u306a\u308a\u5bff\u53f8\u30ac\u30fc\u30eb\u30
6e\u6d99\u3001\u3001\u3001EP/04 - \u6c38\u9060\u306e\u592a\u967d\u3002.mp3", "ti
tle": "??????", "trackNumber": "4", "artist": "0.8?????"}, {"album": "??????????
???EP", "path": "0.8\u79d2\u3068\u885d\u6483\u3002 - \u3044\u306a\u308a\u5bff\u5
3f8\u30ac\u30fc\u30eb\u306e\u6d99\u3001\u3001\u3001EP/05 - CraZy, Faster, CraZy.
mp3", "title": "CraZy, Faster, CraZy", "trackNumber": "5", "artist": "0.8?????"}
]

0 个答案:

没有答案