2 years ago
#42992
JetskiS
Write loop values to csv file
I am working with Abaqus-python and I would like to write a lot of data to a csv file. I want to fill multiple columns with my loops. However I don't manage to write loops to my csv file, only the first row. This is my code;
import csv
# Define list with with partnames
PartNames = ["C7", "T1", "T2", "T3", "T4", "T5", "T6", "T7", "T8", "T9", "T10", "T11", "T12",
"L1", "L2", "L3", "L4", "L5", "S1"]
# Create lists to save data
PartLevelLeft, aLabels, axcoords, aycoords, azcoords = [], [], [], [], []
for i in range(len(PartNames)-1): # S1 does not contain screws
PartLevelLeft = mdb.models[modelname].rootAssembly.sets["Screw['" + PartNames[i] + "_L']"]
for curNode in PartLevelLeft.nodes:
partlabelL.append(curNode.label)
partxcordL.append(curNode.coordinates[0])
partycordL.append(curNode.coordinates[1])
partzcordL.append(curNode.coordinates[2])
aLabels.append(partlabelL)
axcoords.append(partxcordL)
aycoords.append(partycordL)
azcoords.append(partzcordL)
# Arrays with multiple coordinates of different nodes
pt1L = [axcoords[0][0], aycoords[0][0], azcoords[0][0]]
print('pt1L = ', pt1L, 'pt1PartlabelL = ', pt1PartlabelL, 'pt1InstanceName = ', "Screw['" + PartNames[i] + "_L']")
This prints arrays with different values, I show three printed rows for example;
('pt1L = ', [31.1655006408691, -118.636100769043, -846.924499511719], 'pt1PartlabelL = ', 904, 'pt1InstanceName = ', "Screw['L5_L']")
('pt1L = ', [41.5686988830566, -137.057601928711, -815.5546875], 'pt1PartlabelL = ', 1561, 'pt1InstanceName = ', "Screw['L4_L']")
('pt1L = ', [46.5876007080078, -138.033294677734, -776.843994140625], 'pt1PartlabelL = ', 3583, 'pt1InstanceName = ', "Screw['L3_L']")
But now I want to write this data into a CSV file using the following code;
header = ['pt1PartlabelL', 'pt1L_x ', 'pt1L_y ', 'pt1L_z ']
data = ["Screw['" + PartNames[i] + "_L']", pt1L[0], pt1L[1], pt1L[2]]
# open the file in the write mode
with open(r'C:\1. Abaqus tijdelijk\211214 find displacement\NIEUW ALLES\220112 write to excel.csv', 'wb') as f:
# create the csv writer
writer = csv.writer(f)
# write a row to the csv file
writer.writerow(header)
for row in data:
# write a row to the csv file
writer.writerow(data)
It only prints the first array of pt1L multiple times, like this;
Can someone help me with the code to have my loops in the CSV file? Thank you in advance!
python
csv
abaqus
0 Answers
Your Answer