python如何將由多個用戶信息組成的段落按不同的用戶拆分開來?
問題描述
Name: a002ID: ffd7eb90-3705-4248-8c21-f3d579ccc54dDisplay Name: Email: .com.cnFirst Name: a002Last Name: liDepartment: Title: Description: Account Disabled: falseAccount Unlocked At: 2017-04-24 07:25:08ZName: adminID: c41cc2dd-8fbf-4dc2-a5a6-99e6738952dfDisplay Name: Email: First Name: adminLast Name: Department: Title: Description: Account Disabled: falseAccount Unlocked At: 1970-01-01 00:00:00ZName: xuanID: 38cb2ab5-0969-4ace-9555-9909e331a174Display Name: Email: First Name: xuanLast Name: LiangDepartment: Title: Description: Account Disabled: falseAccount Unlocked At: 2017-05-04 01:44:24ZName: a001ID: 6b45403d-4654-4e0a-9145-91405d67aa3bDisplay Name: Email: com.cnFirst Name: a001Last Name: liDepartment: Title: Description: Account Disabled: falseAccount Unlocked At: 2017-04-24 10:09:33Z
如上面的文段,按不同的Name來區(qū)分不同的用戶,最終可以把不同用戶的信息分別存入mysql?
問題解答
回答1:f.readline()讀到Name: xxxx 就表示進入下個用戶信息
回答2:mysql 里設置 name 約束 unique, 然后一條條插入數(shù)據(jù)庫就可以了
回答3:# coding: utf8from collections import defaultdictfile_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 = tmpif i.startswith(’Name’): user_name = key continueif user_name: result[user_name][key] = valueprint result # 用戶結果集合字典, 可以遍歷這個插入數(shù)據(jù)庫, 也能在運行中插入, 任君選擇回答4:
用正則分割,再根據(jù)用戶名分到一組
DATA = re.findall(r’(.*?ddZ)’, a, re.S)for i in DATA: print(i) print(’----------------------’)
相關文章:
1. javascript - ionic1的插件如何遷移到ionic2的項目中2. java - 如何在Fragment中調(diào)用Activity的onNewIntent?3. javascript - h5上的手機號默認沒有識別4. mysql里的大表用mycat做水平拆分,是不是要先手動分好,再配置mycat5. css - 關于input標簽disabled問題6. python - 獲取到的數(shù)據(jù)生成新的mysql表7. 怎么用css截取字符?8. window下mysql中文亂碼怎么解決??9. javascript - jquery hide()方法無效10. python的文件讀寫問題?
