【问题标题】:Python parsing file into dictionary of list of lists: for loop is only appending last linePython将文件解析为列表列表的字典:for循环仅附加最后一行
【发布时间】:2018-12-06 17:40:11
【问题描述】:

我正在尝试解析具有一致格式的文件:一个标题和几行按间距分隔的文本。 当一行有一个值时,我想启动一个新的字典键,将以下行读入列表列表,每个列表都是拆分词。 我首先尝试使用this 来尝试让程序识别新标记并使用索引计数器设置新键。 然后我最初使用this 相应地拆分行。

这是我的代码目前的样子:

import sys

def openfile(file):
    frames = {}
    index = 0
    with open(file, 'r') as f:
        for line in f:
            if line.strip() == '5310':
                index +=1
            else:
                newline = line
                print newline
                frames[index] = []
                frames[index].append([newline.split()])
        print frames

openfile(sys.argv[1])

索引将正确计数并且“打印换行符”正在打印我想要的所有行,但最终打印的字典是一个嵌套列表:

{1:[['last', 'line', 'of', 'input', 'file']]}

我想要的是:

{1:[[line1],[line2] ...], 2:[[nextline], [nextline] ...], ... , key n : [[line], [line]....[lastline]]}

我也试过了:

def openfile(file):
    frames = {}
    index = 0
    with open(file) as f:
         for line in f:
            if str(line.strip()) == '5310':
                index += 1
            else:
                frames[index] = []
                frames[index].append([line.split()])
    return frames

这也行不通。 这给我留下了两个问题: 1:为什么我当前的代码会打印但不附加我想要的行? 2. 我还能尝试什么来让它发挥作用?

编辑 谢谢!我设法让它工作。 如果有人遇到类似的问题,这是我的有效代码:

import sys

def openfile(file):
    frames = {}
    index = 0
    with open(file, 'r') as f:
        for line in f:
            if line.strip() == '5310':
                index +=1
                frames[index] = []
            else:
                newline = line
                print newline
                frames[index].append([newline.split()])
        print frames

openfile(sys.argv[1])

【问题讨论】:

  • 我想你可能想看看defaultdictframes[index] = [] 擦除针对该键存储的所有值。如果不为此设置测试用例,我将使用 frames = defaultdict(list) 并从循环中删除 frames[index] = []。这行得通吗?
  • frames[index].append([newline.split()]) 使frames[index] 成为列表列表的列表。使用extend 或删除多余的[...]

标签: python for-loop append fileparsing


【解决方案1】:

你的问题很明显......一旦你看到问题:-)

            frames[index] = []
            frames[index].append([newline.split()])

每次循环时,您都会清除较早的进度,并从一个新的空列表开始。因此,只有最后一次迭代的结果在 frames 中。

在进入循环之前,初始化代码只需执行一次。

with open(file) as f:
     frames[index] = []
     for line in f:

...或其他适合您的应用程序的点。

【讨论】:

    猜你喜欢
    • 2019-07-08
    • 2019-05-15
    • 1970-01-01
    • 2018-08-11
    • 1970-01-01
    • 2021-11-26
    • 2021-11-04
    • 2013-08-21
    • 2014-07-06
    相关资源
    最近更新 更多