How to split paragraphs composed of multiple user information by different users in python?
世界只因有你
世界只因有你 2017-05-18 10:51:32
0
4
699

Name: a002
ID: ffd7eb90-3705-4248-8c21-f3d579ccc54d
Display Name:
Email: .com.cn
First Name: a002
Last Name: li
Department:
Title:
Description:
Account Disabled: false
Account Unlocked At: 2017-04-24 07:25:08Z
Name: admin
ID: c41cc2dd-8fbf-4dc2-a5a6-99e6738952df
Display Name:
Email:
First Name: admin
Last Name:
Department:
Title:
Description:
Account Disabled: false
Account Unlocked At: 1970-01-01 00:00:00Z
Name: xuan
ID: 38cb2ab5-0969-4ace-9555-9909e331a174
Display Name:
Email:
First Name: xuan
Last Name: Liang
Department:
Title:
Description:
Account Disabled: false
Account Unlocked At: 2017-05-04 01:44:24Z
Name: a001
ID: 6b45403d-4654-4e0a-9145-91405d67aa3b
Display Name:
Email: com.cn
First Name: a001
Last Name: li
Department:
Title:
Description:
Account Disabled: false
Account Unlocked At: 2017-04-24 10:09:33Z

如上面的文段,按不同的Name来区分不同的用户,最终可以把不同用户的信息分别存入mysql?

世界只因有你
世界只因有你

reply all(4)
左手右手慢动作

f.readline()
Reading Name: xxxx means entering the next user information

我想大声告诉你

Set the name constraint unique in mysql, and then insert it into the database one by one

淡淡烟草味
# coding: utf8
from collections import defaultdict
file_name = '1.txt'
result = defaultdict(dict)
with open(file_name) as f:
    user_name = ''
    for i in f:
        tmp = i.strip().split(':', 1)  # 只切割一次
        if len(tmp) == 1:
            # 对应的键没有值, 用空字符补充
            tmp.append('')
        key, value = tmp
        if i.startswith('Name'):
            user_name = key
            continue
        if user_name:
            result[user_name][key] = value
print result # 用户结果集合字典, 可以遍历这个插入数据库, 也能在运行中插入, 任君选择
某草草

Use regular expressions to divide, and then group them into groups based on usernames

DATA = re.findall(r'(.*?\d\dZ)', a, re.S)
for i in DATA:
    print(i)
    print('----------------------')
    

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template