【发布时间】: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])
【问题讨论】:
-
我想你可能想看看
defaultdict。frames[index] = []擦除针对该键存储的所有值。如果不为此设置测试用例,我将使用frames = defaultdict(list)并从循环中删除frames[index] = []。这行得通吗? -
frames[index].append([newline.split()])使frames[index]成为列表列表的列表。使用extend或删除多余的[...]
标签: python for-loop append fileparsing