Windowsの場合、Python 2.7を使用して.PLMファイルからディレクトリ名を抽出しようとしています.PLMファイルは、パナソニックのボイスレコーダー。ボイスレコーディングのディレクトリ名を保存します。Python:ユニコード文字列のバイナリファイル(.PLM)の検索
(例:「HelloÆØÅ」フォルダに保存したい音声録音があるとすると、このボイスレコーダは「SV_VC001」というフォルダと「SD_VOICE.PLM」という名前のフォルダを作成します。
私はDaneですので、アスキーではサポートされていないÆ、Ø、Åの文字を使用するので、変換する必要がありますこのバイナリデータをユニコードに変換します。
これまでのところ、ディレクトリの名前は56バイト以降から格納され、0のバイトで終了することがわかりました。
322d 332d 3135 20c5 7274 6964 7320 6b72
6564 736c f862 6574 206d 6963 6861 656c
これはコードがある私は、これまで使用しています:
例えば、一つの記録と呼ばれるディレクトリに格納されている六角値を持っている、「2-3-15Årstidsはマイケルをkredsløbet」# Finds the filename in the .PLM-file
def FindFileName(File):
# Opens the file and points to byte 56, where the file name starts
f = open(File,'rb')
f.seek(56)
Name = ""
byte = f.read(1) # Reads the first byte after byte 56
while byte != "\x00": # Runs the loop, until a NUL-character is found (00 is NUL in hex)
Name += str(byte) # Appends the current byte to the string Name
byte = f.read(1) # reads the next byte
f.close()
return Name
これは機能します - ディレクトリ名はASCII文字のみを使用しています( 'æ'、 'ø'または 'å'はありません)。
ただし、文字列にUnicode文字がある場合、これは他の文字に変換されます。 "2-3-15Årstidskredsløbetmichael"というディレクトリで、このプログラムは "2-3-15紅葉のマイケル賭け"
あなたは何か提案がありますか?
ありがとうございます。次のようにマーク身代金、コードからの提案を追加
EDIT
です。また、疑問符をスペースに変更し、\ xc5と\ xd8(それぞれÅとØの16進数)をåとøに変更しました。
WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect: u'C:\\Users\\UserName\\Desktop\\TestDir\\Mapper\\13-10*14 ESSOTERISK \xc5NDSSTR\xd8MNIN'
が文字列 "13-10 * 14 ESSOTERISKÅNDSSTRØMNIN" にする必要がありますが、ÅとØ(16進C5とD8:大文字Æ、ØとÅのために、次のエラーが発生し
def FindFileName(File):
# Opens the file and points to byte 56, where the file name starts
f = open(File,'rb')
f.seek(56)
Name = ""
byte = f.read(1) # Reads the first byte after byte 56
while byte and (byte != "\x00"): # Runs the loop, until a NUL-character is found (00 is NUL in hex)
# Since there are problems with "?" in directory names, we change those to spaces
if byte == "?":
Name += " "
elif byte == "\xc5":
Name += "å"
elif byte == "\xd8":
Name += "ø"
else:
Name += byte
byte = f.read(1) # reads the next byte
f.close()
return Name.decode('mbcs')
)がエラーを投げています。
まだ問題は発生しません。だから**名前**には 'æ'、 'ø'、または 'å'が含まれていません。 – HelloWorld
これはPython 2またはPython 3ですか?違いがあります。 –
このWindowsとエンコーディングは何ですか?文字列はWindowsコードページのエンコードであり、ユニコードではないと思います。 – tdelaney