How to eliminate unwanted spaces - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How to eliminate unwanted spaces (/thread-27335.html) |
How to eliminate unwanted spaces - Mohan - Jun-03-2020 Hi, I am new to python. I have a device. This device gives serial data. When I read the serial data using python I can see that data is something like below. \000026hi \000026hi there are lot space coming with the serial data. I can not modify the data from the serial device. In the python script I need to eliminate the spaces and print only the data and compare that data with our data and print if matches. here is my code. This code prints the data from serial device to the file using python. here I took a variable called scan1 and put my data. If the serial data matches to the variable scan1 then print the data with good read message. from __future__ import print_function import serial, time, io, datetime from serial import Serial import time #ser = serial.Serial('/dev/ttyUSB0',115200) addr = "COM10" ## serial port to read data from baud = 115200 ## baud rate for instrument ser = serial.Serial( port = addr,\ baudrate = baud,\ parity=serial.PARITY_NONE,\ stopbits=serial.STOPBITS_ONE,\ bytesize=serial.EIGHTBITS,\ timeout=0) print("Connected to: " + ser.portstr) filename="Scanner_Data.txt" f = open("Scanner_Data.txt", "a") scan1 = "000026hi" while True: with open("Scanner_Data.txt", "a") as f: s = ser.readline() line = s.decode('utf-8').replace('\r\n', '') time.sleep(1) print(line) if (line) == scan1: print("Good Read") f.write(line + "Good Read" + "\r\n") f.close() f.write(line + "\r\n") f.close()The output is It is printing \000026hi \000026hi the if statement is always false. I knew it is due to the spaces. Can some one help me with eliminating the spaces and printing only the data without spaces and compare with my variable. Thank you. RE: How to eliminate unwanted spaces - menator01 - Jun-03-2020 You could try the strip() function.
RE: How to eliminate unwanted spaces - buran - Jun-03-2020 couple of things to check: line-ending may not be '\r\n' - use strip() as advised by @menator01also, bu printing line on line 32 - even if line is empty string, the print will always ass new line, so this may be what you getyou can replace line 32 with print(repr(line))this will show what you print or you can try if line: print(line)this way it will print only if line is not empty str. RE: How to eliminate unwanted spaces - Mohan - Jun-04-2020 Hi, That issue is solved. I have a new issue. When I print the data I see the data as I needed. However when I compare the data the if is always false. Please have a look below code. from __future__ import print_function import serial, time, io, datetime from serial import Serial import time #ser = serial.Serial('/dev/ttyUSB0',115200) addr = "COM10" ## serial port to read data from baud = 115200 ## baud rate for instrument ser = serial.Serial( port = addr,\ baudrate = baud,\ parity=serial.PARITY_NONE,\ stopbits=serial.STOPBITS_ONE,\ bytesize=serial.EIGHTBITS,\ timeout=0) print("Connected to: " + ser.portstr) filename="Scanner_Data.txt" f = open("Scanner_Data.txt", "a") scan1 = "https://conf.de/lg/88bf-328aea0a75" scan1_u = u"https://conf.de/lg/88bf-328aea0a75\n" while True: with open("Scanner_Data.txt", "a") as f: s = ser.readline() line = s.decode('utf-8').replace('\r\n', '') time.sleep(1) #print(line) if (line) == scan1_u: print("Good Read") f.write(line + "Good Read" + "\r\n") f.close() f.write(line + "\r\n") f.close()The data in the variable line is exactly my data. However when I compare the same data from variable scan1 or scan_u I dont see the if statement becoming true. RE: How to eliminate unwanted spaces - snippsat - Jun-04-2020 Mohan Wrote:The data in the variable line is exactly my data.As mention use repr() to see all.So line 33 uncomment to this: print(repr(line)) RE: How to eliminate unwanted spaces - buran - Jun-04-2020 Also note there is quite a lot redundant code - e.g. on line 20 you define a variable for your file name, yet you work/open hardcoded name (you don't use the variable) on line 21 you open the file, then again open it on line 29, using with context manager. Remove line 21, keep line 29, remove lines 37 and 39 - when using with it takes care to close the file for youYou write line to file always, and when it match - you also print on console 'Good read', thus line 36 is also redundant. just keep line 38. Also, I would open the file for writing before the loop with open(filename, 'a') as f: while True: # rest of code |