英文:
Organize itens from txt file in a list
问题
从一个文本文件作为输入文件开始,我需要将这些行组织到一个必须包含相同特性的列表中。
给定具有这些行的文本文件:
SimulationControl,
Yes, !- Do Zone Sizing Calculation
Yes, !- Do System Sizing Calculation
Building,
Building, !- Name
0, !- North Axis {deg}
Suburbs, !- Terrain
0.04, !- Loads Convergence Tolerance Value {W}
SizingPeriod:DesignDay,
Summer Design Day in Modelagem WoodFrame (01-01:02-01) Dec, !- Name
1, !- Month
15, !- Day of Month
SummerDesignDay, !- Day Type
SizingPeriod:DesignDay,
Winter Design Day in Modelagem WoodFrame (01-01:02-01), !- Name
6, !- Month
15, !- Day of Month
我需要一个列表:
building = [
Building,
Building, !- Name
0, !- North Axis {deg}
Suburbs, !- Terrain
0.04, !- Loads Convergence Tolerance Value {W}
]
sizingperiod = [
SizingPeriod:DesignDay,
Summer Design Day in Modelagem WoodFrame (01-01:02-01) Dec, !- Name
1, !- Month
15, !- Day of Month
SummerDesignDay, !- Day Type
SizingPeriod:DesignDay,
Winter Design Day in Modelagem WoodFrame (01-01:02-01), !- Name
6, !- Month
15, !- Day of Month
]
simulation = [
SimulationControl,
Yes, !- Do Zone Sizing Calculation
Yes, !- Do System Sizing Calculation
]
该文本未排序。
我尝试使用起始和结束字符串来组织这些行,但在某些情况下不起作用。
英文:
starting from a txt file as an input file, I need to organize the lines in a list that must contain the same characteristics.
Given a txt file with these lines:
SimulationControl,
Yes, !- Do Zone Sizing Calculation
Yes, !- Do System Sizing Calculation
Building,
Building, !- Name
0, !- North Axis {deg}
Suburbs, !- Terrain
0.04, !- Loads Convergence Tolerance Value {W}
SizingPeriod:DesignDay,
Summer Design Day in Modelagem WoodFrame (01-01:02-01) Dec, !- Name
1, !- Month
15, !- Day of Month
SummerDesignDay, !- Day Type
SizingPeriod:DesignDay,
Winter Design Day in Modelagem WoodFrame (01-01:02-01), !- Name
6, !- Month
15, !- Day of Month
I need a lists whit:
building = [
Building,
Building, !- Name
0, !- North Axis {deg}
Suburbs, !- Terrain
0.04, !- Loads Convergence Tolerance Value {W}
]
sizingperiod = [
SizingPeriod:DesignDay,
Summer Design Day in Modelagem WoodFrame (01-01:02-01) Dec, !- Name
1, !- Month
15, !- Day of Month
SummerDesignDay, !- Day Type
SizingPeriod:DesignDay,
Winter Design Day in Modelagem WoodFrame (01-01:02-01), !- Name
6, !- Month
15, !- Day of Month
]
simulation = [
SimulationControl,
Yes, !- Do Zone Sizing Calculation
Yes, !- Do System Sizing Calculation
]
The txt is not ordered.
I try to organize the lines whit a start and end strings but in some cases that not work.
答案1
得分: 1
当然 - 这将为您提供一个包含列表的字典,其中包含列表块和行的列表:
import io
from collections import defaultdict
txt_file = io.StringIO("""
SimulationControl,
Yes, !- Do Zone Sizing Calculation
Yes, !- Do System Sizing Calculation
Building,
Building, !- Name
0, !- North Axis {deg}
Suburbs, !- Terrain
0.04, !- Loads Convergence Tolerance Value {W}
SizingPeriod:DesignDay,
Summer Design Day in Modelagem WoodFrame (01-01:02-01) Dec, !- Name
1, !- Month
15, !- Day of Month
SummerDesignDay, !- Day Type
SizingPeriod:DesignDay,
Winter Design Day in Modelagem WoodFrame (01-01:02-01), !- Name
6, !- Month
15, !- Day of Month
""")
parsed = defaultdict(list)
current_key = None
current_entry = None
for line in txt_file:
if line.strip() == "":
continue
if line.startswith(" "):
current_entry.append(line)
else:
current_key = line.strip()
current_entry = []
parsed[current_key].append(current_entry)
print(dict(parsed))
(自然地,txt_file
将是您的 open("my_file.txt")
)
输出如下(在此处进行了良好的格式化以供查看):
{
"Building,": [
[
" Building, !- Name\n",
" 0, !- North Axis {deg}\n",
" Suburbs, !- Terrain\n",
" 0.04, !- Loads Convergence Tolerance Value {W}\n",
]
],
"SimulationControl,": [
[
" Yes, !- Do Zone Sizing Calculation\n",
" Yes, !- Do System Sizing Calculation\n",
]
],
"SizingPeriod:DesignDay,": [
[
" Summer Design Day in Modelagem WoodFrame (01-01:02-01) Dec, !- Name\n",
" 1, !- Month\n",
" 15, !- Day of Month\n",
" SummerDesignDay, !- Day Type\n",
],
[
" Winter Design Day in Modelagem WoodFrame (01-01:02-01), !- Name\n",
" 6, !- Month\n",
" 15, !- Day of Month\n",
],
],
}
英文:
Sure – this'll get you a dict of lists of blocks of lists of lines:
import io
from collections import defaultdict
txt_file = io.StringIO("""
SimulationControl,
Yes, !- Do Zone Sizing Calculation
Yes, !- Do System Sizing Calculation
Building,
Building, !- Name
0, !- North Axis {deg}
Suburbs, !- Terrain
0.04, !- Loads Convergence Tolerance Value {W}
SizingPeriod:DesignDay,
Summer Design Day in Modelagem WoodFrame (01-01:02-01) Dec, !- Name
1, !- Month
15, !- Day of Month
SummerDesignDay, !- Day Type
SizingPeriod:DesignDay,
Winter Design Day in Modelagem WoodFrame (01-01:02-01), !- Name
6, !- Month
15, !- Day of Month
""")
parsed = defaultdict(list)
current_key = None
current_entry = None
for line in txt_file:
if line.strip() == "":
continue
if line.startswith(" "):
current_entry.append(line)
else:
current_key = line.strip()
current_entry = []
parsed[current_key].append(current_entry)
print(dict(parsed))
(naturally txt_file
would be your open("my_file.txt")
)
and the output is (nicely formatted for viewing here)
{
"Building,": [
[
" Building, !- Name\n",
" 0, !- North Axis {deg}\n",
" Suburbs, !- Terrain\n",
" 0.04, !- Loads Convergence Tolerance Value {W}\n",
]
],
"SimulationControl,": [
[
" Yes, !- Do Zone Sizing Calculation\n",
" Yes, !- Do System Sizing Calculation\n",
]
],
"SizingPeriod:DesignDay,": [
[
" Summer Design Day in Modelagem WoodFrame (01-01:02-01) Dec, !- Name\n",
" 1, !- Month\n",
" 15, !- Day of Month\n",
" SummerDesignDay, !- Day Type\n",
],
[
" Winter Design Day in Modelagem WoodFrame (01-01:02-01), !- Name\n",
" 6, !- Month\n",
" 15, !- Day of Month\n",
],
],
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论