Heim  >  Fragen und Antworten  >  Hauptteil

Wie teile ich in Python Absätze auf, die aus mehreren Benutzerinformationen verschiedener Benutzer bestehen?

Name: a002
ID: ffd7eb90-3705-4248-8c21-f3d579ccc54d
Anzeigename:
E-Mail: .com.cn
Vorname: a002
Nachname: li
Abteilung:
Titel:
Beschreibung:
Konto fähig: false
Konto freigeschaltet am: 24.04.2017 07:25:08Z
Name: admin
ID: c41cc2dd-8fbf-4dc2-a5a6-99e6738952df
Anzeigename:
E-Mail:
Vorname: admin
Nachname:
Abteilung :
Titel:
Beschreibung:
Konto deaktiviert: false
Konto freigeschaltet am: 1970-01-01 00:00:00Z
Name: xuan
ID: 38cb2ab5-0969-4ace-9555-9909e331a174
Anzeigename:
E-Mail:
Vorname: xuan
Nachname: Liang
Abteilung:
Titel:
Beschreibung:
Konto deaktiviert: false
Konto freigeschaltet am: 04.05.2017 01:44:24Z
Name: a001
ID: 6b45403d-4654- 4e0a-9145-91405d67aa3b
Anzeigename:
E-Mail: com.cn
Vorname: a001
Nachname: li
Abteilung:
Titel:
Beschreibung:
Konto deaktiviert: false
Konto freigeschaltet am: 24.04.2017 10 :09:33Z

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

世界只因有你世界只因有你2712 Tage vor630

Antworte allen(4)Ich werde antworten

  • ringa_lee

    ringa_lee2017-05-18 10:53:32

    f.readline()
    读到Name: xxxx 就表示进入下个用户信息

    Antwort
    0
  • 我想大声告诉你

    我想大声告诉你2017-05-18 10:53:32

    mysql 里设置 name 约束 unique, 然后一条条插入数据库就可以了

    Antwort
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-18 10:53:32

    # 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 # 用户结果集合字典, 可以遍历这个插入数据库, 也能在运行中插入, 任君选择

    Antwort
    0
  • 某草草

    某草草2017-05-18 10:53:32

    用正则分割,再根据用户名分到一组

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

    Antwort
    0
  • StornierenAntwort