风-fmgao

导航

user_add示例

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    : 2018/5/28 16:51
# @File    : use_test_add.py

数据:test.txt

1,Alex Li,Alex Li,13651054608,Market,2013-04-01
2,Jack Wang,28,13451024618,HR,2015-01-07
3,Rain Wang,21,13451054628,Market,2017-04-01
4,Mack Qiao,44,15653354238,Sales,2016-02-01
5,Rachel Chen,23,13351024606,Market,2013-03-16
6,Eric Liu,19,18531054602,Marketing,2012-12-01
8,Kevin Chen,22,13151054603,Sales,2013-04-01
9,Shit Wen,20,13351024642,Market,2017-07-03
10,Shanshan Du,26,13698424612,Operation,2017-07-02
11,Alex Li,25,134435344,IT,2015‐10‐29
12,Alex Li,25,1344353441,IT,2015‐10‐29
13,Alex Li,25,1344353443,IT,2015‐10‐29
14,Alex Li,25,1344353424,IT,2015‐10‐29


方法一:


staff_table = {}
field_list = ["num", "name", "age", "phone", "dept", "enroll_date"]

def openfile():
    staff_file = open("test.txt", 'r+', encoding='utf-8')
    for line in staff_file:
        line = line.strip().split(',')
        staff_table[line[1]] = dict(zip(field_list, line[:6]))


if __name__ == "__main__":
    openfile()
    print(staff_table, type(staff_table))
    # {'Alex Li': {'num': '14', 'name': 'Alex Li', 'age': '25', 'phone': '1344353424', 'dept': 'IT', 'enroll_date': '2015‐10‐29'}, 'Jack Wang': {'num': '2', 'name': 'Jack Wang', 'age': '28', 'phone': '13451024618', 'dept': 'HR', 'enroll_date': '2015-01-07'}, 'Rain Wang': {'num': '3', 'name': 'Rain Wang', 'age': '21', 'phone': '13451054628', 'dept': 'Market', 'enroll_date': '2017-04-01'}, 'Mack Qiao': {'num': '4', 'name': 'Mack Qiao', 'age': '44', 'phone': '15653354238', 'dept': 'Sales', 'enroll_date': '2016-02-01'}, 'Rachel Chen': {'num': '5', 'name': 'Rachel Chen', 'age': '23', 'phone': '13351024606', 'dept': 'Market', 'enroll_date': '2013-03-16'}, 'Eric Liu': {'num': '6', 'name': 'Eric Liu', 'age': '19', 'phone': '18531054602', 'dept': 'Marketing', 'enroll_date': '2012-12-01'}, 'Kevin Chen': {'num': '8', 'name': 'Kevin Chen', 'age': '22', 'phone': '13151054603', 'dept': 'Sales', 'enroll_date': '2013-04-01'}, 'Shit Wen': {'num': '9', 'name': 'Shit Wen', 'age': '20', 'phone': '13351024642', 'dept': 'Market', 'enroll_date': '2017-07-03'}, 'Shanshan Du': {'num': '10', 'name': 'Shanshan Du', 'age': '26', 'phone': '13698424612', 'dept': 'Operation', 'enroll_date': '2017-07-02'}} <class 'dict'>

    for k, v in staff_table.items():
        print(k, v, type(v))  #  Alex Li {'num': '14', 'name': 'Alex Li', 'age': '25', 'phone': '1344353424', 'dept': 'IT', 'enroll_date': '2015‐10‐29'} <class 'dict'>

方法二:

#支持以下三种查询方法
#find name,age from staff_table where age > 22
#find * from staff_table where dept = "IT"
#find * from staff_table where enroll_date like "2013"

staff_table = {}
new_staff_table = {}
new_staff_list = []

def openfile():
    staff_file = open("staffinfo.txt", 'r+', encoding='utf-8')
    for line in staff_file:
        line = line.split(',')
        staff_table[line[1]] = line
    for value in staff_table:
        new_staff_table['num'] = staff_table[value][0]
        new_staff_table['name'] = staff_table[value][1]
        new_staff_table['age'] = staff_table[value][2]
        new_staff_table['phone'] = staff_table[value][3]
        new_staff_table['dept'] = staff_table[value][4]
        new_staff_table['enroll_date'] = staff_table[value][5]
        print(staff_table[value])
        staff_table[value] = new_staff_table
        print(staff_table[value])
    print(staff_table)

openfile()

 

posted on 2018-05-28 17:01  风-fmgao  阅读(175)  评论(0编辑  收藏  举报