我想以ICSV 格式保存 的元素。附加了當前和所需的輸出。
import numpy as np
import csv
I=np.array([[0, 1],
[0, 3],
[1, 2],
[1, 4],
[2, 5],
[3, 4],
[3, 6],
[4, 5],
[4, 7],
[5, 8],
[6, 7],
[7, 8]])
with open('Test123.csv', 'w') as f:
writer = csv.writer(f)
# write the data
writer.writerows(I.T)
當前輸出為

所需的輸出是

uj5u.com熱心網友回復:
您可以嘗試使用writerow并創建逗號分隔
import numpy as np
import csv
I=np.array([[0, 1],
[0, 3],
[1, 2],
[1, 4],
[2, 5],
[3, 4],
[3, 6],
[4, 5],
[4, 7],
[5, 8],
[6, 7],
[7, 8]])
with open('Test123.csv', 'w') as f:
writer = csv.writer(f)
# write the data
writer.writerow(map(lambda x: f'{x[0]}, {x[1]}', zip(*I.T)))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/471460.html
