Take the 2-minute tour ×

This question already has an answer here:

Hi I have been reading up about regular expressions, I have got some basic res working. I now have been trying to use Re to sort out data like this:


...into a tuple but I cannot get it to work.

Can anyone explain how they would go about something like this?


share|improve this question
what do you mean by "data like this"? Numeric integers? that sometimes have 3 digits? Whats the pattern you're trying to capture with a regex? –  Doug T. May 3 '11 at 2:33
I meant to separate every integer spaced out by a comma into a list. –  freeload247 May 3 '11 at 2:36
add comment

marked as duplicate by tiago, Mike, Sindre Sorhus, Steve Greatrex, Karl Anderson Aug 5 '13 at 21:20


3 Answers

You don't want regular expressions here.

s = "144,1231693144,26959535291011309493156476344723991336010898738574164086137773096960,26959535291011309493156476344723991336010898738574164086137773096960,1.00,4295032833,1563,2747941 288,1231823695,26959535291011309493156476344723991336010898738574164086137773096960,26959535291011309493156476344723991336010898738574164086137773096960,1.00,4295032833,909,4725008"

print s.split(',')

Gives you:

['144', '1231693144', '26959535291011309493156476344723991336010898738574164086137773096960', '26959535291011309493156476344723991336010898738574164086137773096960', '1.00
', '4295032833', '1563', '2747941 288', '1231823695', '26959535291011309493156476344723991336010898738574164086137773096960', '26959535291011309493156476344723991336010898
738574164086137773096960', '1.00', '4295032833', '909', '4725008']
share|improve this answer
You'll want to use s.strip().split(',') if this is read in from a file. The strip method gets rid of the newline and other whitespace. –  mgold Sep 7 '12 at 20:33
add comment

How about a list?


It might help if you could explain what kind of info we are looking at. Maybe some background info also?


I had a thought you might want the info in groups of two?

then try:

re.split(r"\d*,\d*", mystring)

and also if you want them into tuples [ (pair[0], pair[1]) for match in re.split(r"\d*,\d*", mystring) for pair in match.split(",")]

more readable form:

mylist = []
    for pair in match.split(",")
        mylist.append((pair[0], pair[1]))
share|improve this answer
add comment

Question is a little vague.

list_of_lines = multiple_lines.split("\n")
for line in list_of_lines:
    list_of_items_in_line = line.split(",")
    first_int = int(list_of_items_in_line[0])


share|improve this answer
add comment
