gpt4 book ai didi

python - 如何在Python中读取文档的特定部分

转载 作者:太空宇宙 更新时间:2023-11-03 18:44:28 24 4
gpt4 key购买 nike

我有一个如图所示的文档。我想检索文件 ID、作者和描述。在某些fileid中,没有文件的描述。但在某些fileid(即fileid=3)中,有文件的描述。我想知道如何在Python中获得描述。谢谢。

start=re.compile('<file fileid=(\d+)\s*>')
end=re.compile('</file\s*>')

The Document starts from here:
--------------------------------------
<file fileid=11>
System File 765411
J.K
STAV December, 1959
</file>

<file fileid=12>
Batch File
James P.
STAV December, 1959
</file>

<file fileid=13>
Empty file
Person:Wolfe, P.
STAV December, 1959

This is a description of the empty file.
You need to put this file in the D:\location\systemB.
After putting the file to the location, the system will boot up.
Later, System B will refresh itself and show the output.
</file>

<file fileid=14>
Nomal File
Mercy Lusia
STAV December, 1959
</file>

最佳答案

一个非常简单的方法是读取文件的每一行,直到到达包含以下内容的行

<file fileid=xx> 。然后读取所有数据,直到结束</file>标签

关于python - 如何在Python中读取文档的特定部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19848766/

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