users = ["<USER John Bells (100000469012345)>","<USER Shaym kismet (1027112345)>"]
unsorted_list = []
username_list = []
users_id_list = []
def data_handeling01():
for friends_list in users:
unsorted_list.append(friends_list)
print unsorted_list
for data in unsorted_list:
string01 = data[6::]
splitted_info = string01.split(" (")
username_list.append(str(splitted_info[0]))
users_id_list.append(splitted_info[-1].split(")")[0])
print username_list
print users_id_list
data_handeling01()
**
Traceback (most recent call last):
File "get-formatted-data-lists.py", line 19, in <module>
data_handeling01()
File "get-formatted-data-lists.py", line 12, in data_handeling01
string01 = data.data[6::]
AttributeError: 'User' object has no attribute 'data'
**
我正在寻找的结果是用户名输入username_list = [“John Bells”],用户ID输入users_id_list = [“100000469012345”]
答案 0 :(得分:2)
使用正则表达式:
import re
users = ["<USER John Bells (100000469012345)>","<USER Shaym kismet (1027112345)>"]
username_list = []
users_id_list = []
for i in users:
m = re.search("\<USER\s+(.*?)\((.*?)\)", i)
if m:
username_list.append(m.group(1))
users_id_list.append(m.group(2))
print(username_list)
print(users_id_list)
<强>输出:强>
['John Bells ', 'Shaym kismet ']
['100000469012345', '1027112345']
答案 1 :(得分:0)
对我来说,你的代码工作简单了一点:
users = ["<USER John Bells (100000469012345)>","<USER Shaym kismet (1027112345)>"]
unsorted_list = []
username_list = []
users_id_list = []
def data_handeling01():
for data in users:
string01 = data[6::]
splitted_info = string01.split(" (")
username_list.append(str(splitted_info[0]))
users_id_list.append(splitted_info[-1].split(")")[0])
print username_list
print users_id_list
data_handeling01()
这是结果:
['John Bells', 'Shaym kismet']
['100000469012345', '1027112345']