Issue
I want to save the data in a csv file in a specific format but I am getting an error. The desired output is attached.
import numpy as np
import csv
N=2
Pe = np.array([[[128.22918457, 168.52413295, 209.72343319],
[129.01598287, 179.03716051, 150.68633749],
[131.00688309, 187.42601593, 193.68172751]],
[[ 64.11459228, 84.26206648, 104.86171659],
[ 64.50799144, 89.51858026, 75.34316875],
[ 65.50344155, 93.71300796, 96.84086375]]])
with open('Test123.csv', 'w') as f:
for x in range(0,N):
print([Pe[x]])
writer = csv.writer(f)
# write the data
writer.writerows(zip(x,Pe[x]))
The error is
TypeError: 'int' object is not iterable
The desired output is
Solution
with open('Test123.csv', 'w') as f:
for x in range(0 ,N):
print([Pe[x]])
writer = csv.writer(f)
# write the data
header = [x] * len(Pe[x])
# header = [x] + ['']*(len(Pe[x])-1)
writer.writerows(zip(header, Pe[x]))
In zip function, parameter needs __iter__ function to execute.
So TypeError: 'int' object is not iterable error occured because int object doesn't have this function.
Make x variable to list type then it works.
Answered By - Lazyer
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.