如何在Python中分割这个逗号分隔的字符串?(复制)

时间:2022-09-03 21:47:32

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:

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

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

“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

谢谢

3 个解决方案

#1


38  

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

#2


8  

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

#3


0  

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.

等。

#1


38  

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

#2


8  

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

#3


0  

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.

等。