如何使用python读取txt文件中字典的值。
我之前用字典保存数据在txt中,现在想只要读取字典的值(value)
# 文件内容 [ { 'id': '01','name': '03','gender': 'male', 'age': '25','tel': '1862799' } ]
with open('students.txt', 'r', encoding='utf-8') as f:
# print( "学号: \t姓名: \t性别: \t年龄: \t电话号码:" )
date = f.readline().strip()
date = eval(date)
for i in date:
print("学号: {}\t姓名: {}\t性别: {}\t年龄: {}\t电话号码: {}".format(i.get('id'), i.get('name'), i.get('gender'),
i.get('tel'), i.get('age')))
# print( "{}\t{}\t{}\t{}\t{}".format( i.get('id'),i.get('name'),i.get('gender') ,i.get('tel'),i.get('age') ) )
#-*- coding:utf-8 –*- #
# 读取一个txt文件
def main():
f = open("top.txt", "r")
AllDict={}
top_memory_list=[]
i=0
for line in f:
NumList=[n for n in line.split()]
data=NumList[0:10:9]
i +=1
if i>6:
list1=[data[0]]
list2=[data[1]]
dict1=dict(zip(list1,list2))
AllDict.update(dict1)
#print(AllDict)
print("PID内存使用率降序前十:")
top_memory_list=sorted(AllDict.items(),key=lambda x:float(x[1]),reverse=True)
#print(top_memory_list[0:10])
for j in range(len(top_memory_list[0:10])):
print(top_memory_list[j])
f.close()
if __name__ == "__main__":
main()
import pandas as pd
import numpy as np
import codecs
filepath = r'C:\Users\zhf\Desktop\华为杯\原型系统V10\中间文件\数据集201908\xxxxx209\无tu的数据集\_xxxxx209_来料错误_.txt'
file = codecs.open(filepath, "r", "utf-8")
print(len(file.read()))
file.close()