473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

split string saving screened spaces

Which module to use to do such thing:

"-a -b -c '1 2 3'" -> ["-a", "-b", "-c", "'1 2 3'"]

(i have string, need to pass it to getopt)
Dec 16 '05 #1
3 1263
Sergey wrote:
Which module to use to do such thing: "-a -b -c '1 2 3'" -> ["-a", "-b", "-c", "'1 2 3'"]


import shlex
shlex.split("-a -b -c '1 2 3'")

['-a', '-b', '-c', '1 2 3']

--
Giovanni Bajo
Dec 16 '05 #2

Sergey wrote:
Which module to use to do such thing:

"-a -b -c '1 2 3'" -> ["-a", "-b", "-c", "'1 2 3'"]

(i have string, need to pass it to getopt)

seems like CSV except that the seperator is space. You may check to see
if the CSV module can take space as delimiter.

Dec 16 '05 #3
Pyparsing has built-in quoted string support.
from pyparsing import OneOrMore,Word,alphanums,quotedString

item = Word('-',alphanums) | quotedString | Word(alphanums)
items = OneOrMore(item)

print items.parseString( "-a -b -c '1 2 3' -d 5 -e zork2000" )

gives:
['-a', '-b', '-c', "'1 2 3'", '-d', '5', '-e', 'zork2000']
Download pyparsing at http://pyparsing.sourceforge.net.

-- Paul

Dec 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
5
by: Robert Oschler | last post by:
Hello, Given the following program: -------------- import re x = "The dog ran. The cat eats! The bird flies? Done." l = re.split("", x)
2
by: SL_McManus | last post by:
Hi All; I am fairly new to Perl. I have a file with close to 3000 lines that I would like to split out in a certain way. I would like to put the record type starting in column 1 for 2 spaces,...
2
by: mehul | last post by:
hi all, i am just starting with PERL and had this doubt related to the speical operator +. what it says is that in the line split( / +/,$line); it will not ignore the initial spaces as it starts...
4
by: Itzik | last post by:
can i split this string string str = "aa a - bb-b - ccc" with this delimiter string del = " - " i want recieve 3 items : "aa a" , "bb-b" , "ccc"
4
by: dchan1 | last post by:
String line = " I am new to c# "; How to use Regex to split the string so that It would return a String array with: token = "I" token = "am" token = "new" token =...
3
by: John Salerno | last post by:
This is an example in the book I'm reading: string fullName = " Edward C Koop "; fullName = fullName.Trim(); string names = fullName.Split(' '); string firstName = names; //...
2
by: Martin Arvidsson, Visual Systems AB | last post by:
Hi! I have a string containing this information: Including file: Myfile.fle (C:\Program Files\Application\Source\Myfile.fle) After myfile.fle there are three spaces! Since i use the...
10
by: teddyber | last post by:
Hello, first i'm a newbie to python (but i searched the Internet i swear). i'm looking for some way to split up a string into a list of pairs 'key=value'. This code should be able to handle this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.