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 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行