python如何將由多個(gè)用戶信息組成的段落按不同的用戶拆分開(kāi)來(lái)?
問(wèn)題描述
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來(lái)區(qū)分不同的用戶,最終可以把不同用戶的信息分別存入mysql?
問(wèn)題解答
回答1:f.readline()讀到Name: xxxx 就表示進(jìn)入下個(gè)用戶信息
回答2:mysql 里設(shè)置 name 約束 unique, 然后一條條插入數(shù)據(jù)庫(kù)就可以了
回答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: # 對(duì)應(yīng)的鍵沒(méi)有值, 用空字符補(bǔ)充 tmp.append(’’)key, value = tmpif i.startswith(’Name’): user_name = key continueif user_name: result[user_name][key] = valueprint result # 用戶結(jié)果集合字典, 可以遍歷這個(gè)插入數(shù)據(jù)庫(kù), 也能在運(yùn)行中插入, 任君選擇回答4:
用正則分割,再根據(jù)用戶名分到一組
DATA = re.findall(r’(.*?ddZ)’, a, re.S)for i in DATA: print(i) print(’----------------------’)
相關(guān)文章:
1. 在應(yīng)用配置文件 app.php 中找不到’route_check_cache’配置項(xiàng)2. html按鍵開(kāi)關(guān)如何提交我想需要的值到數(shù)據(jù)庫(kù)3. HTML 5輸入框只能輸入漢字、字母、數(shù)字、標(biāo)點(diǎn)符號(hào)?正則如何寫(xiě)?4. javascript - 請(qǐng)教如何獲取百度貼吧新增的兩個(gè)加密參數(shù)5. gvim - 誰(shuí)有vim里CSS的Indent文件, 能縮進(jìn)@media里面的6. 跟著課件一模一樣的操作使用tp6,出現(xiàn)了錯(cuò)誤7. PHP類屬性聲明?8. javascript - JS請(qǐng)求報(bào)錯(cuò):Unexpected token T in JSON at position 09. objective-c - ios 怎么實(shí)現(xiàn)微信聯(lián)系列表 最好是swift10. java - 安卓接入微信登錄,onCreate不會(huì)執(zhí)行
