Python Forum
How to eliminate unwanted spaces
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to eliminate unwanted spaces
#1
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.
Reply
#2
You could try the strip() function.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
couple of things to check:
line-ending may not be '\r\n' - use strip() as advised by @menator01
also, 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 get
you 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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
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.
Reply
#5
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))
Reply
#6
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 you
You 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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Eliminate entering QR - Whatsapp web automated by selenium akanowhere 1 3,096 Jan-21-2024, 01:12 PM
Last Post: owalahtole
Question Unwanted execution of unittest ThomasFab 9 2,070 Nov-15-2022, 05:33 PM
Last Post: snippsat
  Removing the unwanted data from a file jehoshua 14 4,094 Feb-01-2022, 09:56 PM
Last Post: jehoshua
  HELP on Unwanted CSV Export Output | Using Selenium to Scrape soothsayerpg 0 1,275 Jun-13-2021, 12:23 PM
Last Post: soothsayerpg
  How to eliminate magic squares formed by the same numbers, but permuted frame 7 3,635 May-09-2019, 11:28 AM
Last Post: frame
  Unwanted delay between looped synth plays WolfeCreek 1 2,324 Aug-02-2018, 09:24 PM
Last Post: Vysero
  Unwanted variable change in module dannyH 2 2,686 May-08-2018, 05:33 PM
Last Post: dannyH
  Unwanted random generation of scripted Shapes in GASP diemildefreude 3 5,182 Oct-23-2016, 03:11 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020