简体   繁体   English

如何在python中将字符串拆分为多个部分?

[英]How can I split a string into multiple parts in python?

I'm having problems splitting my names up by their listed order, name and their exam score. 我在按列出的顺序,姓名和考试成绩分割我的名字时遇到了问题。

I did the following in my code: print repr(names) in a loop to get the following 6 lines of data 我在我的代码中执行了以下操作:在循环中print repr(names)以获得以下6行数据

'1 Name 559/1 '
'2 Name 484/1 '
'3 Name N'ame 444/2 '
'4 Name 400/1 '
'5 Name Name 928/5 '
'6 Name Name-Name 1292/10 '

I want to be able to split them up but I get an error saying AttributeError: 'str' object has no attribute 'lsplit' 我希望能够拆分它们但是我得到一个错误说AttributeError: 'str' object has no attribute 'lsplit'

I've had a go using lsplit, rsplit and split but I can't get it to work... 我有一个使用lsplit,rsplit和拆分,但我无法让它工作......

In the end result names will be turned into 3 variables: names_index , name and names_score 最后,结果名称将变为3个变量: names_indexnamenames_score

Anyone know how I can acheive this? 任何人都知道如何实现这一目标吗?

Thanks - Hyflex 谢谢 - Hyflex

EDIT 编辑

for item in listitems:
    if item.find("strong"):
        names = str(item.text)
        names = items .split("\n")
        for name in names:
            clean_name = name.lstrip(" ")
            print repr(clean_name)
            student_number = clean_name.lsplit(" ", 1)
            student_score = clean_name.rsplit(" ", 1)
            #student_name = clean_name.lsplit(" ", 1) # Unsure how to get the last part..

There is no str.lsplit because str.split already goes from the left. 没有str.lsplit因为str.split已经从左边开始了。

For each string, just call .split() and you will get a list with [names_index, name, names_score] 对于每个字符串,只需调用.split() ,您将获得一个包含[names_index, name, names_score]

If the pattern is number / words / more numbers , then you can use regular expressions: 如果模式是number / words / more numbers ,那么您可以使用正则表达式:

>>> import re
>>> filter(None, re.split(r'(\d+) (\w.*) (\d.*)', '1 Name 559/1 '))
['1', 'Name', '559/1 ']
>>> filter(None, re.split(r'(\d+) (\w.*) (\d.*)', '6 Name Name-Name 1292/10 '))
['6', 'Name Name-Name', '1292/10 ']

rsplit returns the splitted result, not only the last part. rsplit返回分割结果,而不仅仅是最后一部分。 So you are actually going in the right direction. 所以你实际上正朝着正确的方向前进。

For your code where you might have space in your name, you can do this: 对于您的名称中可能包含空格的代码,您可以执行以下操作:

the_string = the_string.strip()
[id, name_score] = the_string.split(' ',1) # Split into ['1', 'name name 654/1']
[name, score] = name_score.rsplit(' ',1)   # Split into ['name name','654/1']

so, for input `6 Name Name-Name 1292/10' your intended values are already there: 因此,对于输入“6 Name Name-Name 1292/10”,您的预期值已经存在:

id # this is '6'
name # this is 'Name Name-name'
score # this is '1292/10'

Its as easy as - 它很简单 -

>>> s = '4 Name 400/1 '
>>> names_index, name, names_score = s.split()
>>> names_index, name, names_score
('4', 'Name', '400/1')
>>> 

As Haidro said, there is no function as lsplit, use split instead. 正如Haidro所说,没有lsplit的功能,而是使用split。

you can operate this way with your list 你可以用你的清单这样操作

>>> names = ['1 Name 559/1 ',
'2 Name 484/1 ',
'3 Name Naaame 444/2 ',
'4 Name 400/1 ',
'5 Name Name 928/5 ',
'6 Name Name-Name 1292/10 ']
>>> names_lst=[]
>>> for n in names:
    names_lst+=n.split()

gives

>>> names_lst
['1', 'Name', '559/1', '2', 'Name', '484/1', '3', 'Name', 'Naaame', 
 '444/2', '4', 'Name', '400/1', '5', 'Name', 'Name', '928/5', '6', 'Name', 
 'Name-Name', '1292/10', ['1', 'Name', '559/1'], ['2', 'Name', '484/1'], 
 ['3', 'Name', 'Naaame', '444/2'], ['4', 'Name', '400/1'],
 ['5', 'Name', 'Name', '928/5'], ['6', 'Name', 'Name-Name', '1292/10']]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM