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


Messages In This Thread
How to eliminate unwanted spaces - by Mohan - Jun-03-2020, 04:02 PM
RE: How to eliminate unwanted spaces - by menator01 - Jun-03-2020, 05:53 PM
RE: How to eliminate unwanted spaces - by buran - Jun-03-2020, 05:59 PM
RE: How to eliminate unwanted spaces - by Mohan - Jun-04-2020, 06:49 AM
RE: How to eliminate unwanted spaces - by snippsat - Jun-04-2020, 08:13 AM
RE: How to eliminate unwanted spaces - by buran - Jun-04-2020, 08:34 AM

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