0
我想使用python和pyexiv2来读取IPTC数据。如何正确解码乱码的UTF-8字符串?
import pyexiv2
image = pyexiv2.Image('test.jpg')
image.readMetadata()
print image['Iptc.Application2.Caption']
这给了我thefollwing:
Copyright: Michael Huebner, Kontakt: +4915100000000xxxxxx Höxx (30) ist im Streit mit dem Arbeitsamt in Brandenburg, xxxxxxxxxxxxxx , xxxxxx,
但它应该给我:
Kinder: Axxxxx Hxxxxx (10) und Exxxxxx Höxx (5), Rxxxxxxx Höxx (30) ist im Streit mit dem Arbeitsamt in Brandenburg, xxxxxxxxxxxxx , xxxxxxxxxxx,
Copyright: Michael Huebner, Kontakt: +4915100000000
这是一个有点乱,因为我不得不删除个人数据,但你可以看看发生了什么:'换行'使得最后一部分覆盖字符串的第一部分。
但现在会很奇怪:
for i in str(image['Iptc.Application2.Caption']):
print i,
这只是打印出所有的字符,包括以正确的顺序换行。但它会弄乱“变音符号”字符。
此:
unicode(image['Iptc.Application2.Caption'])
给我:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 41: ordinal not in range(128)
那么,如何可以兼得:元音变音和正确的字符串奥德?我怎样才能修复这个字符串?
,如果你需要在输出换行符不要使用块引用。 –
'print repr(image ['Iptc.Application2.Caption'])'show是否在字符串中?你可能在那里有一个'\ r',一个回车符。 –
是的,它是\ r。我怎样才能解决这个问题? – JasonTS