zoukankan      html  css  js  c++  java
  • python读取文本中存储的测试数据:练习

    '''
    读取文本中存储的测试数据:
    数据如下
    url:https://www.runoob.com/python/att-string-strip.html,mobilephone:13709878989,pwd:12345
    url:https://www.cnblogs.com/passagain/p/11447413.html,mobilephone:18889878989,pwd:33333

    要求如下
    将每一行的数据存到一个字典里,且'url 、mobilephone 、pwd'为key
    对应key后面的值为value,再将所有字典后存在一个列表里
    '''
    def read_data(file):
    list_1 = [] # 定义一个空列表,接收每一行数据存储的字典
    file = open(file, 'r+', encoding='UTF-8')
    # 利用redlines去读取每一行数据,返回的结果是列表
    # for循环遍历读出的列表的数据,根据逗号分割:split(',')分割次数默认所有的
    for items in file.readlines():
    dict_1 = {} # 定义一个空字典去接收key_value
    for n in items.strip(' ').split(','):
    dict_1[n.split(':',1)[0]] = n.split(':',1)[1]
    list_1.append(dict_1)
    file.close()
    return list_1


    print(read_data('test_file'))
  • 相关阅读:
    LCT 动态树 模板
    [HNOI2010] 物品调度 fsk
    [HNOI2010] 矩阵 matrix
    [HNOI2010] 平面图判定 planar
    [HNOI2010] 公交线路 bus
    [HNOI2017]抛硬币
    [HNOI2010] 弹飞绵羊 bounce
    [HNOI2010] 合唱队 chorus
    [HNOI2017]礼物
    [HNOI2017]大佬
  • 原文地址:https://www.cnblogs.com/hongyufei/p/12381530.html
Copyright © 2011-2022 走看看