问题遇到的现象和发生背景
最近在用python编写一个机器人的正解程序,通过读取数据进行正解计算,并将计算出的数据输出到文档
用代码块功能插入代码,请勿粘贴截图
import numpy as np
import math
import csv
with open("tjx.csv", mode="r", encoding="utf-8") as f,\
open("tt.txt", mode="w", encoding="utf-8") as f1:
for row in csv.reader(f, skipinitialspace=True):
c1 = float(row[0])
c2 = float(row[1])
c3 = float(row[2])
c4 = float(row[3])
c5 = float(row[4])
c6 = float(row[5])
T1 = np.mat([[round(math.cos(c1)), -round(math.sin(c1)), 0, 0],
[round(math.sin(c1)), round(math.cos(c1)), 0, 0],
[0, 0, 1, 187],
[0, 0, 0, 1]])
T2 = np.mat([[round(math.cos(c2), 4), 0, round(math.sin(c2), 4), 0],
[round(math.sin(c2), 4), 0, -round(math.cos(c2), 4), 0],
[0, 1, 0, 6],
[0, 0, 0, 1]])
T3 = np.mat([[round(math.cos(c3), 4), -round(math.sin(c3), 4), 0, 210*(round(math.cos(c3), 4))],
[round(math.sin(c3), 4), round(math.cos(c3), 4), 0, 210*(round(math.sin(c3), 4))],
[0, 0, 1, 0],
[0, 0, 0, 1]])
T4 = np.mat([[round(math.cos(c4), 4), 0, -round(math.sin(c4), 4), 0],
[round(math.sin(c4), 4), 0, round(math.cos(c4), 4), 0],
[0, -1, 0, 210.5],
[0, 0, 0, 1]])
T5 = np.mat([[round(math.cos(c5), 4), 0, round(math.sin(c5), 4), 0],
[round(math.sin(c5), 4), 0, -round(math.cos(c5), 4), 0],
[0, 1, 0, 0],
[0, 0, 0, 1]])
T6 = np.mat([[round(math.cos(c6), 4), 0, -round(math.sin(c6), 4), 0],
[round(math.sin(c6), 4), 0, round(math.cos(c6), 4), 0],
[0, -1, 0, 159.3],
[0, 0, 0, 1]])
T01 = np.dot(T1, T2)
T12 = np.dot(T01, T3)
T23 = np.dot(T12, T4)
T34 = np.dot(T23, T5)
T45 = np.dot(T34, T6)
for i in range(len(T45)):
f1.write(str(T45[i]) + '\n')
运行结果及报错内容
程序能够正常运行,结果可以正常输出,但是运行结果前有空格。
我的解答思路和尝试过的方法
使用 strip 但是未能清楚两端空格
我想要达到的结果
输出的结果没有空格存在