如何在Python中拆分这个以逗号分隔的字符串?

时间:2011-05-03 02:31:38

标签: python regex string

您好,我一直在阅读正则表达式,我有一些基本的res工作。我现在一直在尝试使用Re来整理这样的数据:

  

“144,1231693144,26959535291011309493156476344723991336010898738574164086137773096960,26959535291011309493156476344723991336010898738574164086137773096960,1.00,4295032833,1563,2747941288,1231823695,26959535291011309493156476344723991336010898738574164086137773096960,26959535291011309493156476344723991336010898738574164086137773096960,1.00,4295032833,909,4725008”

...进入一个元组,但我无法让它工作。

任何人都可以解释他们会怎么做这样的事情?

由于

3 个答案:

答案 0 :(得分:43)

你不想要正则表达式。

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

print s.split(',')

给你:

['144', '1231693144', '26959535291011309493156476344723991336010898738574164086137773096960', '26959535291011309493156476344723991336010898738574164086137773096960', '1.00
', '4295032833', '1563', '2747941 288', '1231823695', '26959535291011309493156476344723991336010898738574164086137773096960', '26959535291011309493156476344723991336010898
738574164086137773096960', '1.00', '4295032833', '909', '4725008']

答案 1 :(得分:8)

列表怎么样?

mystring.split(",")

如果您能解释我们正在查看的信息类型,可能会有所帮助。也许还有一些背景信息?

编辑:

我想到你可能想要两组一组的信息?

然后尝试:

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

以及如果你想让他们进入元组

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

以更易阅读的形式:

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

答案 2 :(得分:1)

问题有点模糊。

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

相关问题