gpt4 book ai didi

python - 循环并加载 yaml 文件的压缩文件夹

转载 作者:行者123 更新时间:2023-12-01 07:34:57 34 4
gpt4 key购买 nike

我有一个包含 15000 个 yaml 文件的压缩文件夹。我想使用 yaml.safe_load 迭代该文件夹,以便每个文件都采用字典格式,并且我可以从每个文件中提取我需要的信息。到目前为止,我已经使用 zipfile.ZipFile 和 yaml.safe_load 编写了一些代码,但它仅适用于压缩文件夹中的第一个文件。有人介意看一下并解释一下我的误解吗?

zip_file = zipfile.ZipFile("D:/export.zip")
files = zip_file.namelist()
print(files)
for i in range(10):
with zip_file.open(files[i]) as yamlfile:
yamlreader = yaml.safe_load(yamlfile)
print(yamlreader["identifier"])

现在我只是迭代 10 个文件以使生活更轻松。最终我想做全部 15 000 个。“identifier”是 yaml 文件中的一个键。

这是错误:

10.5281/zenodo.1014773
Traceback (most recent call last):
File "C:/Users/estho/PycharmProjects/GSOC3/testing_dataextraction.py", line 20, in <module>
yamlreader = yaml.safe_load(yamlfile)
File "C:\Users\estho\PycharmProjects\GSOC3\lib\site-packages\yaml\__init__.py", line 162, in safe_load
return load(stream, SafeLoader)
File "C:\Users\estho\PycharmProjects\GSOC3\lib\site-packages\yaml\__init__.py", line 114, in load
return loader.get_single_data()
File "C:\Users\estho\PycharmProjects\GSOC3\lib\site-packages\yaml\constructor.py", line 41, in get_single_data
node = self.get_single_node()
File "C:\Users\estho\PycharmProjects\GSOC3\lib\site-packages\yaml\composer.py", line 36, in get_single_node
document = self.compose_document()
File "C:\Users\estho\PycharmProjects\GSOC3\lib\site-packages\yaml\composer.py", line 55, in compose_document
node = self.compose_node(None, None)
File "C:\Users\estho\PycharmProjects\GSOC3\lib\site-packages\yaml\composer.py", line 84, in compose_node
node = self.compose_mapping_node(anchor)
File "C:\Users\estho\PycharmProjects\GSOC3\lib\site-packages\yaml\composer.py", line 127, in compose_mapping_node
while not self.check_event(MappingEndEvent):
File "C:\Users\estho\PycharmProjects\GSOC3\lib\site-packages\yaml\parser.py", line 98, in check_event
self.current_event = self.state()
File "C:\Users\estho\PycharmProjects\GSOC3\lib\site-packages\yaml\parser.py", line 428, in parse_block_mapping_key
if self.check_token(KeyToken):
File "C:\Users\estho\PycharmProjects\GSOC3\lib\site-packages\yaml\scanner.py", line 116, in check_token
self.fetch_more_tokens()
File "C:\Users\estho\PycharmProjects\GSOC3\lib\site-packages\yaml\scanner.py", line 260, in fetch_more_tokens
self.get_mark())
yaml.scanner.ScannerError: while scanning for the next token
found character '\t' that cannot start any token
in "yamlfile_10_5281_zenodo_1745362.yaml", line 4, column 1

谢谢。

最佳答案

在我看来,文件“yamlfile_10_5281_zenodo_1745362.yaml”中有一个错误的 token 名称。尝试在没有此文件的情况下运行它。在 python 中 \t 代表制表符,因此通常不能包含在字符串 ect 中而不转义它。

关于python - 循环并加载 yaml 文件的压缩文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57029540/

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