hi people !
<newbie>
i got some trouble with regular expressions
i need to split a string like this on the ',' character :
mystring = ""\test1, test2\", test, 42"
i wanna get something (a list) like this (3 elements) :
"test1, test2"
test
42
but the only thing i get is a list like this (4 elements) :
"test1"
"test2"
test
42
each element is separated by ',' but 1st element which is delimited by
'"' may contain ',' character inside.
so the regular expression i need is something like :
split each element using ',' delimiter but if ',' delimiter is included
between '"' please do not split
</newbie>
1st question is : does someone has understood the question ?
2nd question is : does someone has an answer ?
thanks people
scott 4 1075
Maybe, you need the csv module:
import csv
mystring = "\"test1, test2\", test, 42"
# The one argument to csv.reader is an iterable object
# You could use a file here...
csv_reader = csv.reader([mystring])
for line in csv_reader:
print line
['test1, test2', ' test', ' 42']
hth,
jw
On 6/28/05, scott <sc***@alussina n.org> wrote: hi people ! <newbie> i got some trouble with regular expressions i need to split a string like this on the ',' character : mystring = ""\test1, test2\", test, 42" i wanna get something (a list) like this (3 elements) : "test1, test2" test 42 but the only thing i get is a list like this (4 elements) : "test1" "test2" test 42 each element is separated by ',' but 1st element which is delimited by '"' may contain ',' character inside. so the regular expression i need is something like : split each element using ',' delimiter but if ',' delimiter is included between '"' please do not split </newbie> 1st question is : does someone has understood the question ? 2nd question is : does someone has an answer ? thanks people scott -- http://mail.python.org/mailman/listinfo/python-list
Doh - please note that csv.reader takes more than one argument - the
FIRST one is an iterable object.
jw
On 6/28/05, Jaime Wyant <pr***********@ gmail.com> wrote: Maybe, you need the csv module: import csv mystring = "\"test1, test2\", test, 42" # The one argument to csv.reader is an iterable object # You could use a file here... csv_reader = csv.reader([mystring]) for line in csv_reader: print line ['test1, test2', ' test', ' 42'] hth, jw On 6/28/05, scott <sc***@alussina n.org> wrote: hi people !
<newbie> i got some trouble with regular expressions i need to split a string like this on the ',' character :
mystring = ""\test1, test2\", test, 42"
i wanna get something (a list) like this (3 elements) : "test1, test2" test 42
but the only thing i get is a list like this (4 elements) : "test1" "test2" test 42
each element is separated by ',' but 1st element which is delimited by '"' may contain ',' character inside.
so the regular expression i need is something like : split each element using ',' delimiter but if ',' delimiter is included between '"' please do not split </newbie>
1st question is : does someone has understood the question ? 2nd question is : does someone has an answer ?
thanks people
scott -- http://mail.python.org/mailman/listinfo/python-list
re.findall(r'"? (.+?)"?(?:,|$)' , yourtexthere)
Oh, oops, sorry, that code doesn't respect the quotes.
Use this code:
re.findall(r'(" .+?"|\S+)(?:,|$ )', yourtexthere) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Raymond Hettinger |
last post by:
Peter Norvig's creative thinking triggered renewed interest in PEP 289.
That led to a number of contributors helping to re-work the pep details
into a form that has been well received on the python-dev list:
http://www.python.org/peps/pep-0289.html
In brief, the PEP proposes a list comprehension style syntax for
creating fast, memory...
|
by: jens |
last post by:
I was wondering if anyone could tell be whether it's possible to
instruct preg_match not to save subexpressions matches under 0, 1...n
keys when using named subexpressions; I.e instruct preg_named to
matched under their names only,
and not under their numbers. E.g. take the following example:
<pre style="width:100%;text-align:justify">...
|
by: Sehboo |
last post by:
Hi,
I have several regular expressions that I need to run against
documents. Is it possible to combine several expressions in one
expression in Regex object. So that it is faster, or will I have to
use all the expressions seperately?
Here are my regular expressions that check for valid email address and
link
Dim Expression As String =
|
by: Együd Csaba |
last post by:
Hi All,
I'd like to "compress" the following two filter expressions into one -
assuming that it makes sense regarding query execution performance.
.... where (adate LIKE "2004.01.10 __:30" or adate LIKE "2004.01.10 __:15")
....
into something like this:
.... where adate LIKE "2004.01.10 __:(30/15)" ...
|
by: aarklon |
last post by:
Hi all,
Recently i was reading a c book which contained a section called C
pitfalls.
it had a paragraph on the following lines:-
-------------------------------------------------------------------
using nonconstant expressions in an initializer list
-------------------------------------------------------------------
| |
by: R. Rajesh Jeba Anbiah |
last post by:
I just checked the new GG Beta and came across this interesting ratings
(ratings given by regulars/newbies to c.l.php regulars). I guess this
might surprise for some.
Poster - Avg - No. of ratings
---------------------------------------------
Chung Leong - 4/5 - 46 ratings
Jerry Stuckle - 3/5 - 175 ratings...
|
by: strangerdream |
last post by:
I'm a lurker here, and I do not find any female regulars here in
c.l.c.
Is it something thats got to do with their genes?
|
by: Allan Ebdrup |
last post by:
I have a dynamic list of regular expressions, the expressions don't change
very often but they can change. And I have a single string that I want to
match the regular expressions against and find the first regular expression
that matches the string.
I've gor the regular expressions ordered so that the highest priority is
first (if two or more...
|
by: lyle fairfield |
last post by:
My observation is that over the past few months many of our regulars
seems to be drifting away, making fewer contributions or none at all.
Why?
I can speculate:
1. The quality of questions here in CDMA has been low;
2. The regulars are busy with paying work;
3. Contributing to the MS groups may result in recognition in the form
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |