34

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

razlebe
  • 7,134
  • 6
  • 42
  • 57
freeload247
  • 365
  • 1
  • 3
  • 3
  • 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? – Doug T. May 03 '11 at 02:33
  • I meant to separate every integer spaced out by a comma into a list. – freeload247 May 03 '11 at 02:36

3 Answers3

67

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']
Acorn
  • 49,061
  • 27
  • 133
  • 172
  • 12
    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 07 '12 at 20:33
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]))
James Khoury
  • 21,330
  • 4
  • 34
  • 65
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.

elliot42
  • 3,694
  • 3
  • 26
  • 27