Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
split string
#1
Hi

I've a question about proper method to split a string.

my strings are like this:

object Host "serviceXXX-YYYY" {
I'm doing my plit like this:

hostname = line.split(" ")[2].split('"')[1]
Do you have a better method to do this split?
Thanks for your help

Alex
Reply
#2
Alternatively you can do only one split:

>>> s = 'object Host "serviceXXX-YYYY" {'
>>> s.split('"', maxsplit=2)[1]                                                 
'serviceXXX-YYYY' 
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Split string between two different delimiters, with exceptions DreamingInsanity 2 282 Aug-24-2020, 08:23 AM
Last Post: DreamingInsanity
  Split string with multiple delimiters and keep the string in "groups" DreamingInsanity 4 536 May-12-2020, 09:31 AM
Last Post: DeaD_EyE
  Split a long string into other strings with no delimiters/characters krewlaz 4 558 Nov-15-2019, 02:48 PM
Last Post: ichabod801
  input string split Eric7Giants 3 765 Nov-13-2019, 07:19 PM
Last Post: Gribouillis
  [split] capitalize dict keys for display in string newbieAuggie2019 3 701 Oct-10-2019, 10:50 AM
Last Post: perfringo
  How to split string after delimiter SriRajesh 1 485 Sep-11-2019, 11:25 AM
Last Post: metulburr
  split string and keep separator wardancer84 2 966 Nov-21-2018, 11:20 AM
Last Post: wardancer84
  How to reliably split string containing multiple sqlite statements? shanepy 2 880 Nov-21-2018, 12:07 AM
Last Post: shanepy
  How to split a string containing function calls? Metalman488 4 1,047 Oct-27-2018, 06:50 PM
Last Post: Metalman488
  Encode string split problem sripylearn 3 1,327 Aug-14-2018, 02:42 PM
Last Post: buran

Forum Jump:

User Panel Messages

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