gpt4 book ai didi

python - 从嵌套字典中的文件中读取初始未知数量的 N 行,并在第 N+1 行处开始下一次迭代

转载 作者:行者123 更新时间:2023-12-01 05:40:32 30 4
gpt4 key购买 nike

我想处理一个文本文件(逐行)。 (最初未知)数量的连续行属于同一实体(即它们与该行携带相同的标识符)。例如:

line1: stuff, stuff2, stuff3, ID1, stuff4, stuff5
line2: stuff, stuff2, stuff3, ID1, stuff4, stuff5
line3: stuff, stuff2, stuff3, ID1, stuff4, stuff5
line4: stuff, stuff2, stuff3, ID2, stuff4, stuff5
line5: stuff, stuff2, stuff3, ID2, stuff4, stuff5
...

在此虚拟行中,第 1-3 行属于实体 ID1,第 4-5 行属于实体 ID2。我想将每一行作为字典读取,然后将它们嵌套到包含 IDX 所有字典的字典中(例如,字典 ID1 分别包含第 1-3 行的 3 个嵌套字典)。

更具体地说,我想定义一个函数:

  1. 打开文件
  2. 将实体 ID1 的所有(但仅)行读取到各个字典中
  3. 返回包含 ID1 行的嵌套字典的字典

我希望能够稍后再次调用该函数,以读取下一个字典中以下标识符(ID2)和稍后的 ID3 等的所有行。我遇到的问题之一是我需要在每一行中测试我当前的行是否仍然带有感兴趣的 ID 或者已经是一个新的 ID。如果它是新的,我当然可以停止并返回字典,但在下一轮(例如 ID2)中,ID2 的第一行已经被读取,因此我似乎丢失了该行。

换句话说:一旦遇到具有新 ID 的行,我想以某种方式重置函数中的计数器,以便在下一次迭代中具有新 ID 的第一行不会丢失。

这似乎是一项简单的任务,但我无法找到一种优雅的方法。目前,我在函数之间传递一些“内存”标志/变量,以便跟踪新 ID 的第一行是否已在先前的迭代中读取。这相当庞大且容易出错。

感谢您的阅读...任何想法/提示都非常感谢。如果有些地方不清楚,请询问。

这是我的“解决方案”。它似乎可以正确打印字典(尽管我确信有一种更优雅的方法可以做到这一点)。我还忘了提及该文本文件非常大,因此我想逐个 ID 地处理它,而不是将整个文件读入内存。

with open(infile, "r") as f:
newIDLine = None
for line in f:
if not line:
break
# the following function returns the ID
ID = get_ID_from_line(line)
counter = 1
ID_Dic = dict()
# if first line is completely new (i.e. first line in infile)
if newIDLine is None:
currID = ID
# the following function returns the line as a dic
ID_Dic[counter] = process_line(line)
# if first line of new ID was already read in
# the previous "while" iteration (see below).
if newIDLine is not None:
# if the current "line" is of the same ID then the
# previous one: put previous and current line in
# the same dic and start the while loop.
if ID == oldID:
ID_Dic[counter] = process_line(newIDLine)
counter += 1
ID_Dic[counter] = process_line(line)
currID = ID
# iterate over the following lines until file end or
# new ID starts. In the latter case: keep the info in
# objects newIDline and oldID
while True:
newLine = next(f)
if not newLine:
break
ID = get_ID_from_line(newLine)
if ID == currID:
counter += 1
ID_Dic[counter] = process_line(newLine)
# new ID; save line for the upcomming ID dic
if not ID == currID:
newIDLine = newLine
oldID = ID
break
# at this point it would be great to return the Dictionary of
# the current ID to the calling function but at return to this
# function continue where I left off.
print ID_Dic

最佳答案

如果您希望此函数为每个 id 延迟返回一个字典,您应该使用yield 而不是 return 使其成为生成器函数。在每个 id 的末尾,生成该 id 的字典。然后您可以迭代该生成器。

要处理该文件,请编写一个迭代源的生成器函数,除非您向它发送一个值,在这种情况下,它接下来返回该值,然后返回迭代。 (例如,这是我为自己执行此操作而编写的一个模块: politer.py 。)

如果您不想要的话,您可以通过将值“发回”来轻松解决此问题:

with open(infile, 'r') as f:
polite_f = politer(f)
current_id = None
while True:
id_dict = {}
for i, line in enumerate(polite_f):
id = get_id_from_line(line)
if id != current_id:
polite_f.send(line)
break
else:
id_dict[i] = process_line(line)
if current_id is not None:
yield id_dict
current_id = id

请注意,这会使状态处理在其所属的生成器中保持抽象。

关于python - 从嵌套字典中的文件中读取初始未知数量的 N 行,并在第 N+1 行处开始下一次迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17655390/

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