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

  • SEARCH
  • Home
  • 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 8443961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:16:49+00:00 2026-06-10T09:16:49+00:00

Receive a string from somewhere and the string is a sequence of params. Params

  • 0

Receive a string from somewhere and the string is a sequence of params. Params are separated by whitespace. The task is parse the string to a param list, all params are type string.

For example:

input : "3 45 5.5 a bc"
output : ["3","45","5.5","a","bc"]

Things become little complicated if need to transfer a string include a whitespace, use " to quote.

input: "3 45 5.5 \"This is a sentence.\" bc"
output: ["3","45","5.5","This is a sentence.","bc"]

But what if the sentence happened to include a quote mark? Use the escape character: \" -> ", \\ -> \

input: "3 45 5.5 \"\\\"Yes\\\\No?\\\" it said.\" bc"
output: ['3','45','5.5','"Yes\\NO?" it said.','bc']

Does python have an elegant way to do this job?

PS. I don’t think regular expressions can work this out.

  • 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-06-10T09:16:51+00:00Added an answer on June 10, 2026 at 9:16 am

    Use the shlex.split() function:

    >>> import shlex
    >>> shlex.split("3 45 5.5 a bc")
    ['3', '45', '5.5', 'a', 'bc']
    >>> shlex.split("3 45 5.5 \"This is a sentence.\" bc")
    ['3', '45', '5.5', 'This is a sentence.', 'bc']
    >>> shlex.split("3 45 5.5 \"\\\"Yes\\\\No?\\\" it said.\" bc")
    ['3', '45', '5.5', '"Yes\\No?" it said.', 'bc']
    

    You can create a customizable parser with the shlex.shlex function, then alter it’s behaviour by setting its attributes. For example, you can set the .whitespace attribute to ', \t\r\n' to allow commas to delimit words as well. Then simply convert the shlex instance back to a list to split the input.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I receive a string from a database query, then I remove all HTML tags,
I;m trying to parse a json string which I receive from web server as
I write the following code in simulate.jsp , to receive some String simulationInfos from
how would I format a datetime string received from MySQL? For example, when I
I want to know how to receive the string from a file in Java
I receive a string like this from a third-party service: >>> s '\\u0e4f\\u032f\\u0361\\u0e4f' I
In my small android application I created a Service to receive some string from
I receive a string from jQuery UI Sortable plugin, it gives me a string
I have a system where I receive a string from an outside library, and
I receive a date time as a string from the server that might look

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.