Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6741885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:46:41+00:00 2026-05-26T11:46:41+00:00

When using the Python string function split(), does anybody have a nifty trick to

  • 0

When using the Python string function split(), does anybody have a nifty trick to treat items surrounded by double-quotes as a non-splitting word?

Say I want to split only on white space and I have this:

>>> myStr = 'A B\t"C" DE "FE"\t\t"GH I JK L" "" ""\t"O P   Q" R'
>>> myStr.split()
['A', 'B', '"C"', 'DE', '"FE"', '"GH', 'I', 'JK', 'L"', '""', '""', '"O', 'P', 'Q"', 'R']

I’d like to treat anything within double-quotes as a single word, even if white spaces are embedded, so would like to end up with the below:

['A', 'B', 'C', 'DE', 'FE', 'GH I JK L', '', '', 'O P   Q', 'R']

Or at least this and then I’ll strip off the double-quotes:

['A', 'B', '"C"', 'DE', '"FE"', '"GH I JK L"', '""', '""', '"O P   Q"', 'R']

Any non-regex suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:46:41+00:00Added an answer on May 26, 2026 at 11:46 am

    You won’t be able to get this behaviour with str.split(). If you can live with the rather complex parsing it does (like ignoring double quotes preceded by a back slash), shlex.split() might be what you are looking for:

    >>> shlex.split(myStr)
    ['A', 'B', 'C', 'DE', 'FE', 'GH I JK L', '', '', 'O P   Q', 'R']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to call Python's time.strftime() function using a Unicode format string: u'%d\u200f/%m\u200f/%Y
I'm using python, and I want a function that takes a string containing a
I am using the Python join function to create a string e.g. a =
A trivial CSV line could be spitted using string split function. But some lines
Using Python I want to randomly rearrange sections of a string based on a
Using Python I need to insert a newline character into a string every 64
Using Python I need to delete all characters in a multiline string up to
Using Python, how would I go about reading in (be from a string, file
How to parse the string {'result':(Boolean, MessageString)} using Python regular expressions to get Boolean
I need to extract parts of a string using regex in Python. I'm good

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.