一個(gè)包含5行6關(guān)節(jié)機(jī)器人的關(guān)節(jié)值的csv文件可能是這樣的:
2.950000, -1.570000, 0.000000, 0.000000, 0.000000, 0.000000
2.987484, -1.540050, 0.019967, 0.019992, 0.012495, 0.009996
3.024875, -1.510399, 0.039734, 0.039933, 0.024958, 0.019967
3.062079, -1.481344, 0.059104, 0.059775, 0.037360, 0.029888
3.099002, -1.453174, 0.077884, 0.079468, 0.049667, 0.039734
python代碼實(shí)現(xiàn):
import numpy as np
# 隨機(jī)生成 3x4 矩陣(float), 3x1 向量(int)
y = np.random.rand(3, 4)
d = np.random.randint(-100, 100, 3)
# 以寫(xiě)模式打開(kāi)文件
f = open("output.csv", "w")
# 如果是追加模式窑业,這樣寫(xiě)
#f = open("output.csv", "a")
# 循環(huán)寫(xiě)入
for i in range(len(d)):
output = " %10.6f, %10.6f, %10.6f, %10.6f, %d\n" % (y[i,0], y[i,1], y[i,2], y[i,3], d[i])
f.write(output)
# 關(guān)閉文件
f.close()
以上代碼會(huì)生成一個(gè)三行4列的csv文件:
output.csv
0.097075, 0.762762, 0.722070, 0.905547, 53
0.517850, 0.191464, 0.313131, 0.144955, -29
0.932447, 0.719380, 0.854865, 0.610632, 72