zoukankan      html  css  js  c++  java
  • Python文本操作2

    # list3 = [
    # {"name": "alex", "hobby": "抽烟"},
    # {"name": "alex", "hobby": "喝酒"},
    # {"name": "alex", "hobby": "烫头"},
    # {"name": "alex", "hobby": "Massage"},
    # {"name": "wusir", "hobby": "喊麦"},
    # {"name": "wusir", "hobby": "街舞"},
    # {"name": "taibai", "hobby": "捏脚"},
    # {"name": "taibai", "hobby": "抠脚"},]

    list4 = []
    # list4 = [
    # {"name": "alex", "hobby_list": ["抽烟", "喝酒", "烫头", "Massage"]},
    # {"name": "wusir", "hobby_list": ["喊麦", "街舞"]},
    # ]

    # 思路
    # for el in list3: # el : {"name": "alex", "hobby": "抽烟"},
    # for el_l4 in list4:
    # if el['name'] == el_l4['name']:
    # el_l4['hobby_list'].append(el['hobby'])
    # break
    # else: # list4中没有这个元素
    # list4.append({'name': el['name'], "hobby_list":[el['hobby']] })
    # print(list4)

    # li = [1, 3, 4, "alex", [3, 7, 8, "TaiBai"], 5, "RiTiAn"]
    # # 循环打印列表中的每个元素,遇到列表则再循环打印出它的元素。
    # for i in li:
    # if type(i) == list:
    # for el in i:
    # if type(el) == str:
    # print(el)
    # else:
    # print(el)
    # else:
    # if type(i) == str:
    # print(i)
    # else:
    # print(i)
    '''
    编号,名称,价格,数量,仓库,phone
    1,榴莲,500,60000,1号仓库,10010
    2,苹果,700,70000,2号仓库,10086
    1,榴莲,500,60000,1号仓库,155
    2,苹果,700,70000,2号仓库,166
    '''
    '''变成
    [{'编号': '1', '名称': '榴莲', '价格': '500', '数量': '60000', '仓库': '1号仓库', 'phone': '10010'},
    {'编号': '2', '名称': '苹果', '价格': '700', '数量': '70000', '仓库': '2号仓库', 'phone': '10086'},
    {'编号': '1', '名称': '榴莲', '价格': '500', '数量': '60000', '仓库': '1号仓库', 'phone': '155'},
    {'编号': '2', '名称': '苹果', '价格': '700', '数量': '70000', '仓库': '2号仓库', 'phone': '166'}]
    '''
    f = open("水果.data", mode="r", encoding="utf-8")
    titles = f.readline().strip() # 读取第一行 id,name,price,num
    t_list = titles.split(",") # 【id,name,price,num】
    lst = []
    for line in f: # "1,苹果,500,60000" {id:1,name:liulian, num:xx, price:xxx}
    dic = {}
    ll = line.strip().split(",")
    for i in range(len(t_list)):
    dic[t_list[i]] = ll[i]
    lst.append(dic)
    f.close()
    print(lst)
  • 相关阅读:
    Kubernetes 部署Mysql 主从复制集群
    Kubernetes Kube-proxy
    vRO Extend VirtualDisk Workflow
    Kubernetes Horizontal Pod Autoscaler
    vRO 7 添加RestHost证书报错
    Kubernetes Resource Qoutas
    Kubernetes 命令补全
    Kubernetes 部署Weave Scope监控
    Kubernetes Yaml
    Go reflect反射
  • 原文地址:https://www.cnblogs.com/searchforyou/p/9925549.html
Copyright © 2011-2022 走看看