gpt4 book ai didi

python-3.x - Python 3.4 pliSTLib 不起作用(str vs bytes 错误)

转载 作者:行者123 更新时间:2023-12-01 03:06:54 24 4
gpt4 key购买 nike

所以,我开始了一个新的玩具项目,并决定第一次使用 Python 3...

In [1]: import plistlib

In [2]: with open("/Volumes/Thunderbay/CURRENT/Music/iTunes/iTunes Library.xml") as itl:
library = plistlib.load(itl)
...:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-6459a022cb71> in <module>()
1 with open("/Volumes/Thunderbay/CURRENT/Music/iTunes/iTunes Library.xml") as itl:
----> 2 library = plistlib.load(itl)
3

/usr/local/Cellar/python3/3.4.3_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/plistlib.py in load(fp, fmt, use_builtin_types, dict_type)
984 fp.seek(0)
985 for info in _FORMATS.values():
--> 986 if info['detect'](header):
987 P = info['parser']
988 break

/usr/local/Cellar/python3/3.4.3_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/plistlib.py in _is_fmt_xml(header)
556
557 for pfx in prefixes:
--> 558 if header.startswith(pfx):
559 return True
560

TypeError: startswith first arg must be str or a tuple of str, not bytes
嗯好吧,让我们给它一个提示:
In [3]: with open("/Volumes/Thunderbay/CURRENT/Music/iTunes/iTunes Library.xml") as itl:
library = plistlib.load(itl, fmt=plistlib.FMT_XML)
...:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-ef5f06b44ec2> in <module>()
1 with open("/Volumes/Thunderbay/CURRENT/Music/iTunes/iTunes Library.xml") as itl:
----> 2 library = plistlib.load(itl, fmt=plistlib.FMT_XML)
3

/usr/local/Cellar/python3/3.4.3_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/plistlib.py in load(fp, fmt, use_builtin_types, dict_type)
995
996 p = P(use_builtin_types=use_builtin_types, dict_type=dict_type)
--> 997 return p.parse(fp)
998
999

/usr/local/Cellar/python3/3.4.3_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/plistlib.py in parse(self, fileobj)
323 self.parser.EndElementHandler = self.handle_end_element
324 self.parser.CharacterDataHandler = self.handle_data
--> 325 self.parser.ParseFile(fileobj)
326 return self.root
327

TypeError: read() did not return a bytes object (type=str)
plistlibstandard library ,但是从上面的问题我感觉它实际上并没有被转换为 Python 3?
无论如何,我的实际问题是:是否可以使用 plistlib 打开 XML plist 文件?在 Python 3.4.3 中?
我肯定在这里遗漏了一些明显的东西……只是注意到了 plistlib 的 Py2 版本。 (有效!)有一个不同的界面,所以有人实际上修改了库的代码以包含在 Py3 中......

最佳答案

感谢@J Presper Eckert 为我提供了有关寻找什么的线索...

然后我找到了这篇文章:
http://python-notes.curiousefficiency.org/en/latest/python3/text_file_processing.html#the-binary-option

这表明答案只是以二进制模式打开文件,尝试它并且它有效!

with open("/Volumes/Thunderbay/CURRENT/Music/iTunes/iTunes Library.xml", 'rb') as itl:
library = plistlib.load(itl)

关于python-3.x - Python 3.4 pliSTLib 不起作用(str vs bytes 错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31233855/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com