Ich stehe wieder mal auf dem Schlauch und komme deshalb nicht weiter. Ich möchte eine Textdatei einlesen und darin die ganzen Elemente scannen und als als eine Kombination von tuple/dict wieder abspeichern.
Die Datei könnte dabei folgendermaßen aussehen:
Das Ergebnis sollte dann so aussehen:**
** elements.inp
**
*Element, elset=set1, Type=CPS3
1, 214, 104, 213
2, 126, 125, 19
3, 203, 127, 128
4, 128, 127, 31
5, 233, 200, 199
*Element, elset=set2, type=CPS4
6, 234, 86, 85, 23
7, 233, 156, 129, 85
8, 191, 78, 77, 34
9, 131, 130, 41, 3
10, 164, 163, 46, 923
**
Code: Alles auswählen
#
# meshfile.py
#
elements = ((CPS3,
{ 1: (214, 104, 213),
2: (126, 125, 19),
3: (203, 127, 128),
4: (128, 127, 31),
5: (233, 200, 199)}),
(CPS4, {6: (234, 86, 85, 23),
7: (233, 156, 129, 85),
8: (191, 78, 77, 34),
9: (131, 130, 41, 3),
10: (164, 163, 46, 923)}), )
Code: Alles auswählen
deck_file = file("elements.inp", "r")
deck_lines = deck_file.readlines()
deck_file.close()
output_file = file("meshfile.py", "w")
output_file.write("}\n\nelements = (")
inline = False
elementlines = []
for line in deck_lines:
if '*element,' in line.lower():
element_type_ = line.strip().split(",")
for t in element_type_:
if "type" in t.lower():
element_type = t.split("=")[-1]
inline = True
elif inline == True and '*' not in line:
elementlines.append(line.strip())
elif inline == True and '*' in line:
inline = False
label_list = []
node_list = []
for line in elementlines:
tokens = line.strip().split(",")
line_values = tuple(map(int, tokens))
label_list.append(line_values[0])
node_list.append( line_values[1:] )
elements = dict(zip(label_list, node_list))
output_file.write("(%s, { " % element_type)
for line in elements.keys():
output_file.write(str(line)+": "+str(elements[line])+",\n\t\t")
output_file.write("}), ")
output_file.write(")")
output_file.close()
Vielen Dank, Calo