[英]Python splitting text into two lines instead of printing one
我正在使用 RedBoard QWIIC 读取传感器数据。 程序输出如下数据:
408 10 45.47 98760.30 23.33 413 19.17
400 7 45.45 98758.38 23.33 414 19.17
415 16 45.45 98757.56 23.33 414 19.17
405 3 45.45 98758.38 23.33 414 19.17
然而,当我运行 Python 程序时,.txt 文件如下所示:
07/21/2022 14:12:49 400 0 45.42 98763.58 23.34 406
19.17
07/21/2022 14:12:52 400 0 45.45 98759.20 23.34 406
19.18
07/21/2022 14:12:55 400 0 45.48 98764.69 23.34 405
19.18
有问题的 Python 程序:
import serial
import time
serialPort_1 = 'COM3'
baud_rate = 9600
write_to_file_path = "test 1 7-21-22.txt"
output_file = open(write_to_file_path, "w+")
ser1 = serial.Serial(serialPort_1, baud_rate, timeout=4)
while 1:
line1 = ser1.readline()
line1 = line1.decode("utf-8")
print(time.strftime("%m/%d/%Y %H:%M:%S") + ' ' + line1)
output_file.write(time.strftime("%m/%d/%Y %H:%M:%S")+' '+line1)
time.sleep(0.00001)
如何让程序停止在这最后两个值之间缩进? 我已经尝试从在传感器输出后打印“\t”字符改为打印几个空格。
我找到了我的解决方案。 这一切都在于传递给 read_until() 命令的换行符分隔符。 对于输入行,我使用:
RedBoardline = RedBoardSerial.read_until()
RedBoardline = RedBoardline.rstrip()
这消除了行尾的 0x0A(又名 ASCII 代码 10 或“\n”)字符。 希望这可以帮助某人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.