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
  doing string split with 2 or more split characters Skaperen 22 2,317 Aug-13-2023, 01:57 AM
Last Post: Skaperen
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 1,054 Aug-01-2023, 09:36 AM
Last Post: Pedroski55
  [split] Parse Nested JSON String in Python mmm07 4 1,413 Mar-28-2023, 06:07 PM
Last Post: snippsat
  Split string using variable found in a list japo85 2 1,237 Jul-11-2022, 08:52 AM
Last Post: japo85
  Split string knob 2 1,840 Nov-19-2021, 10:27 AM
Last Post: ghoul
  Split string between two different delimiters, with exceptions DreamingInsanity 2 1,977 Aug-24-2020, 08:23 AM
Last Post: DreamingInsanity
  Split string with multiple delimiters and keep the string in "groups" DreamingInsanity 4 6,352 May-12-2020, 09:31 AM
Last Post: DeaD_EyE
  Split a long string into other strings with no delimiters/characters krewlaz 4 2,704 Nov-15-2019, 02:48 PM
Last Post: ichabod801
  input string split Eric7Giants 3 2,947 Nov-13-2019, 07:19 PM
Last Post: Gribouillis
  [split] capitalize dict keys for display in string newbieAuggie2019 3 2,937 Oct-10-2019, 10:50 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

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