tianjunxi123 2022-11-28 17:07 采纳率: 100%
浏览 21
已结题

python输出结果有空格

问题遇到的现象和发生背景

最近在用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')


运行结果及报错内容

程序能够正常运行,结果可以正常输出,但是运行结果前有空格。

img

我的解答思路和尝试过的方法

使用 strip 但是未能清楚两端空格

我想要达到的结果

输出的结果没有空格存在

img

  • 写回答

2条回答 默认 最新

  • 於黾 2022-11-28 17:16
    关注

    strip 不改变原来的字符串,你需要把返回值赋值给原来的字符串,或者直接输出strip的结果

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月9日
  • 已采纳回答 12月1日
  • 创建了问题 11月28日

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?