Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Remove Parts of a String
#1
Hello,

I'm quite new to python and am attempting to do some coding on a Raspberry Pi.

I have a sensor that gives an output in the following format.

628.6,339,0.30,1.000


I need to splice it so it just gives me the 628.6

The string isn't always the same length so i need to chop it on the first comma.

I also need to change the decimal point so it looks like this but that should be easy enough once its just a number.

6.286

Thanks
Reply
#2
Hello Dean, I believe you have a couple of options available. You can do:

some_string = "628.6,339,0.30,1.000"
split_string = some_string.split(',')
split_string is now a list of strings:
['628.6', '339', '0.30', '1.000']
So you need the first element of this list:

first_element = split_string[0]
At this point first_element is still a string, but you can convert it to a float and do arithmetics with it.
Reply
#3
Or you can get the first element of returned list at once with split

first_element = some_string.split(',')[0]
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
Thanks j.crater for the solution and thanks wavic for compressing the code.

I ended up using the following line to query my EC sensor and convert the output to from μS/cm to CF

output = float(AtlasI2C().query('r').split(',')[0])/100
Reply
#5
Thanks for reporting back and again welcome to Python! :)
Reply
#6
It's not about compressing the code. It's about how Python works. I just start to clear it for myself.
Welcome!
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  remove gilberishs from a "string" kucingkembar 2 203 Mar-15-2024, 08:51 AM
Last Post: kucingkembar
Smile please help me remove error for string.strip() jamie_01 3 1,151 Oct-14-2022, 07:48 AM
Last Post: Pedroski55
  Remove a space between a string and variable in print sie 5 1,706 Jul-27-2022, 02:36 PM
Last Post: deanhystad
  Matching multiple parts in string fozz 31 6,060 Jun-13-2022, 09:38 AM
Last Post: fozz
  How do I remove spurious "." from a string? Zuhan 7 1,962 Apr-12-2022, 02:06 PM
Last Post: Pedroski55
  How to remove char from string?? ridgerunnersjw 2 2,486 Sep-30-2020, 03:49 PM
Last Post: ridgerunnersjw
  Remove from end of string up to and including some character lbtdne 2 2,286 May-17-2020, 09:24 AM
Last Post: menator01
  Remove escape characters / Unicode characters from string DreamingInsanity 5 13,426 May-15-2020, 01:37 PM
Last Post: snippsat
  Highlight and remove specific string of text itsalmade 5 3,447 Dec-11-2019, 11:58 PM
Last Post: micseydel
  Cannot Remove the Double Quotes on a Certain Word (String) Python BeautifulSoup soothsayerpg 5 6,992 Oct-27-2019, 09:53 AM
Last Post: newbieAuggie2019

Forum Jump:

User Panel Messages

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