情况:AHLTA,一种电子病历,将GUI模板导出为文本。我正在建立一个模板编辑器,需要导入文本文件。每行表示一个GUI元素,并以一个数字开始,该数字标识GUI中的父选项卡。行的顺序不重要。我用的是python3。你知道吗
示例(文件):
1,550,57,730,77,0,32770," |||||||0|0||0|0|||0|||0|0|1|0|0|0|||","F=TimesNewRoman|C=8421504|T=T","Last updated: 2017-05-18"
0,743,4,823,48,0,16384," |||||||0|0||0|0|||0|||0|0|0|0|0|0|||","F=Arial|O=5|B=T","TSWF Navigator:<formLinkInfo><version>1.1</version><templateName>TSWF-Navigator</templateName><templateId>2238487</templateId><templateOwnerName>Department of Defense</templateOwnerName><templateOwnerNcid>33962</templateOwnerNcid></formLinkInfo>"
0,828,4,907,24,0,16384," |||||||0|0||0|0|||0|||0|0|0|0|0|0|||","O=5","CORE:<formLinkInfo><version>1.1</version><templateName>TSWF-CORE</templateName><templateId>1995726</templateId><templateOwnerName>Department of Defense</templateOwnerName><templateOwnerNcid>33962</templateOwnerNcid></formLinkInfo>"
2,25,791,370,811,297285,8961," | || ||||19|80|YCN|0|0|Y|N|0|||0|0|5|0|0|0|||","F=Arial|T=T","Responds to affection~ (by 4 months)"
2,25,871,370,891,297287,8961," | || ||||19|80|YCN|0|0|Y|N|0|||0|0|5|0|0|0|||","F=Arial|T=T","Indicates pleasure and displeasure~ (by 4 months)"
我的目标:我想要一个列表字典,其中键对应于GUI选项卡编号,列表包含以该编号开始的所有行。你知道吗
示例:
0:
0,743,4,823,48,0,16384," |||||||0|0||0|0|||0|||0|0|0|0|0|0|||","F=Arial|O=5|B=T","TSWF Navigator:<formLinkInfo><version>1.1</version><templateName>TSWF-Navigator</templateName><templateId>2238487</templateId><templateOwnerName>Department of Defense</templateOwnerName><templateOwnerNcid>33962</templateOwnerNcid></formLinkInfo>"
0,828,4,907,24,0,16384," |||||||0|0||0|0|||0|||0|0|0|0|0|0|||","O=5","CORE:<formLinkInfo><version>1.1</version><templateName>TSWF-CORE</templateName><templateId>1995726</templateId><templateOwnerName>Department of Defense</templateOwnerName><templateOwnerNcid>33962</templateOwnerNcid></formLinkInfo>"
1:
1,550,57,730,77,0,32770," |||||||0|0||0|0|||0|||0|0|1|0|0|0|||","F=TimesNewRoman|C=8421504|T=T","Last updated: 2017-05-18"
2:
2,25,791,370,811,297285,8961," | || ||||19|80|YCN|0|0|Y|N|0|||0|0|5|0|0|0|||","F=Arial|T=T","Responds to affection~ (by 4 months)"
2,25,871,370,891,297287,8961," | || ||||19|80|YCN|0|0|Y|N|0|||0|0|5|0|0|0|||","F=Arial|T=T","Indicates pleasure and displeasure~ (by 4 months)"
问题:我无法提前创建列表,因为在读取文件之前我不知道有多少选项卡。我尝试在每个选项卡的文件中循环,在临时列表中收集该选项卡的项,然后在转到下一个选项卡之前将列表添加到字典中。为简单起见,简化了示例数据:
theFile = ['1,550,57,730,77', '0,743,4,823,48', '0,828,4,907,24', '2,25,791,370,811', '2,25,871,370,891']
tabCount = 3 # for this example; normally pulled from file header
sortedLines = dict()
for i in range(tabCount):
tempList = []
for line in theFile:
tempList.append(line)
sortedLines.update({tabCount: tempList})
tempList.clear()
print('Dict: ', sortedLines)
for k, v in sortedLines.items():
print('Pair: ' + str(k) + ': ' + '[%s]' % ', '.join(map(str, v)))
这似乎是适当的循环,但我最终得到一个空对:
{3: []}
3: []
摘要:只有在运行时才知道列表的数量,如何创建列表字典?你知道吗
结果
相关问题 更多 >
编程相关推荐