博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
user_add示例
阅读量:7066 次
发布时间:2019-06-28

本文共 3284 字,大约阅读时间需要 10 分钟。

#!/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()

 

转载于:https://www.cnblogs.com/fmgao-technology/p/9101000.html

你可能感兴趣的文章
您应该了解的 Windows Azure 网站在线工具
查看>>
深入Java—String源代码
查看>>
C# Socket系列1
查看>>
vmware 安装dos注意
查看>>
远程调用
查看>>
通用业务系统基础平台(五) 工作流系统
查看>>
H5 App如此强悍,要降薪的恐怕已不只是iOS程序员
查看>>
[国家集训队2012]middle
查看>>
SPOJ 1812 Longest Common Substring II
查看>>
vue双向绑定原理
查看>>
路飞学城-python爬虫密训-第一章
查看>>
Python输入输出练习,运算练习,turtle初步练习
查看>>
Android进阶学习
查看>>
用jedis获取redis连接(集群和非集群状态下)
查看>>
html 调用qq客服
查看>>
初学python,感受和C的不同
查看>>
OpenLDAP在LINUX下的安装说明
查看>>
洛谷P1582 倒水
查看>>
洛谷P3146 [USACO16OPEN]248
查看>>
Codeforces Round #419 (Div. 2) A-E
查看>>