36

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:

"144,1231693144,26959535291011309493156476344723991336010898738574164086137773096960,26959535291011309493156476344723991336010898738574164086137773096960,1.00,4295032833,1563,2747941288,1231823695,26959535291011309493156476344723991336010898738574164086137773096960,26959535291011309493156476344723991336010898738574164086137773096960,1.00,4295032833,909,4725008"

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

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

Thanks

2
  • 1
    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? Commented May 3, 2011 at 2:33
  • I meant to separate every integer spaced out by a comma into a list. Commented May 3, 2011 at 2:36

3 Answers 3

69

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'] 
Sign up to request clarification or add additional context in comments.

1 Comment

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.
18

How about a list?

mystring.split(",") 

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

EDIT:

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(",")] 

in a more readable form:

mylist = [] for match in re.split(r"\d*,\d*", mystring): for pair in match.split(",") mylist.append((pair[0], pair[1])) 

Comments

2

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]) 

etc.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.