I'm reading sensor data with a RedBoard QWIIC. The program outputs data which looks like this:
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
Yet when I run my Python program, the.txt file looks like this:
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
The Python program in question:
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)
How do I get the program to stop indenting between these last two values? I have already tried changing from printing a "\t" character after the sensor outputs to printing a few spaces instead.
I found my solution. It all lies in the newline delimiter passed to the read_until() command. For the input line, I use:
RedBoardline = RedBoardSerial.read_until()
RedBoardline = RedBoardline.rstrip()
This eliminated the 0x0A (aka ASCII code 10 or "\n") character at the end of the line. Hope this helps someone.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.