class_07作業


#!/user/bin/python3
#  -*- coding: utf-8 -*-
# @Time      : 2020/6/1 0001 22:24
# @Author    : lemon_小張
# @Email     :981874735@qq.com
# @File      :class_07作業.py
# TODO
'''
1、有以下數據來自於一個嵌套字典的列表(可自定義這個列表),格式如下:
person_info = [{"name":"yuze", "age": 18, "gender": "男", "hobby": "假正經", "motto": "I am yours"} ,  .... 其他]
創建一個txt文本文件,來添加數據
a.第一行添加如下內容:
name,age,gender,hobby,motto'''
person_info = [{"name": "yuze", "age": 18, "gender": "", "hobby": "假正經", "motto": "I am yours"}]
re = []
with open("小張.txt","w",encoding="utf-8") as file:
    for i in person_info:
        for key in i.keys():
            if key not in re:
                re.append(key)
    file.write(','.join(re))


'''b.從第二行開始,每行添加具體用戶信息,例如:
yuze,17,男,假正經, I am yours
cainiao,18,女,看書,Lemon is best!'''
# person_info = [{"name": "yuze", "age": 17, "gender": "男", "hobby": "假正經", "motto": "I am yours"}, \
#                        {"name": "cainiao", "age": 18, "gender": "女", "hobby": "看書", "motto": "Lemon is best!"}]
# re =[]
person_info = [{"name":"yuze", "age": "17", "gender": "", "hobby": "假正經", "motto": "I am yours"} , {"name":"cainiao", "age": "18", "gender": "", "hobby":"看書", "motto": "Lemon is best!"} ]
re=[]
with open("小張.txt","w",encoding="utf-8") as file_info:
    for iteam in person_info:

        for keys in iteam.keys():
            if keys not in re:
              re.append(keys)

    file_info.write(".".join(re)+'\n')
    file_info.write(",".join(list(person_info[0].values()))+'\n')
    file_info.write(",".join(list(person_info[1].values())))
    file_info.close()


'''編寫如下程序
有兩行數據,存放在txt文件里面(手動建立文件,並添加如下數據):

url:/futureloan/mvc/api/member/register,mobile:18866668888@pwd:123456
url:/futureloan/mvc/api/member/recharge,mobile:18866668888@amount:1000

請利用上課所學知識,把txt里面的兩行內容,取出然后返回如下格式的數據:(可定義函數)

[{'url':'/futureloan/mvc/api/member/register','mobile':'18866668888','pwd':'123456'},
\{'url':'/futureloan/mvc/api/member/recharge','mobile':'18866668888','amount':'1000'}]'''
# with open("test_data.txt","w",encoding="utf-8") as file:
#     file.write("url:/futureloan/mvc/api/member/register,mobile:18866668888@pwd:123456 \
# url:/futureloan/mvc/api/member/recharge,mobile:18866668888@amount:1000")
def read_data(file):
    with open("test_data.txt", "w", encoding="utf-8") as file:
        file.write("url:/futureloan/mvc/api/member/register,mobile:18866668888@pwd:123456 \
    url:/futureloan/mvc/api/member/recharge,mobile:18866668888@amount:1000")
    file=open(file,"r+")
    li =[]
    for iteams in file.readlines():
        dict={}
        for iteam in iteams.strip('\n').split(","):
            dict[iteam.split(":",1)[0]] = iteam.split(":",1)[1]
    li.append(dict)
    file.close()
    return li
read_data("test_data.txt")

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM