text processing

J

jitenshah78

I have string like follow
12560/ABC,12567/BC,123,567,890/JK

I want above string to group like as follow
(12560,ABC)
(12567,BC)
(123,567,890,JK)

i try regular expression i am able to get first two not the third one.
can regular expression given data in different groups
 
M

Marc 'BlackJack' Rintsch

I have string like follow
12560/ABC,12567/BC,123,567,890/JK

I want above string to group like as follow (12560,ABC)
(12567,BC)
(123,567,890,JK)

i try regular expression i am able to get first two not the third one.
can regular expression given data in different groups

Without regular expressions:

def group(string):
result = list()
for item in string.split(','):
if '/' in item:
result.extend(item.split('/'))
yield tuple(result)
result = list()
else:
result.append(item)

def main():
string = '12560/ABC,12567/BC,123,567,890/JK'
print list(group(string))

Ciao,
Marc 'BlackJack' Rintsch
 
K

kib2

You can do it with regexps too :
>------------------------------------------------------------------
import re
to_watch = re.compile(r"(?P<number>\d+)[/](?P<letter>[A-Z]+)")

final_list = to_watch.findall("12560/ABC,12567/BC,123,567,890/JK")

for number,word in final_list :
print "number:%s -- word: %s"%(number,word)
>------------------------------------------------------------------

the output is :

number:12560 -- word: ABC
number:12567 -- word: BC
number:890 -- word: JK

See you,

Kib².
 
M

MRAB

Without regular expressions:

def group(string):
    result = list()
    for item in string.split(','):
        if '/' in item:
            result.extend(item.split('/'))
            yield tuple(result)
            result = list()
        else:
            result.append(item)

def main():
    string = '12560/ABC,12567/BC,123,567,890/JK'
    print list(group(string))
How about:
string = "12560/ABC,12567/BC,123,567,890/JK"
r = re.findall(r"(\d+(?:,\d+)*/\w+)", string)
r ['12560/ABC', '12567/BC', '123,567,890/JK']
[tuple(x.replace(",", "/").split("/")) for x in r]
[('12560', 'ABC'), ('12567', 'BC'), ('123', '567', '890', 'JK')]
 
P

Paul McGuire

I have string like follow
12560/ABC,12567/BC,123,567,890/JK

I want above string to group like as follow
(12560,ABC)
(12567,BC)
(123,567,890,JK)

i try regular expression i am able to get first two not the third one.
can regular expression given data in different groups

Looks like each item is:
- a list of 1 or more integers, in a comma-delimited list
- a slash
- a word composed of alpha characters

And the whole thing is a list of items in a comma-delimited list

Now to implement that in pyparsing:
[('12560', 'ABC'), ('12567', 'BC'), ('123', '567', '890', 'JK')]

Wah-lah! (as one of my wife's 1st graders announced in one of his
school papers)

-- Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top