从具有相同名称的字典列表中获取数据,例如。 [{路线号码:123,},{满意的顾客:1.5},{路线号码:456},{满意的顾客:0}]
我在这方面遇到了一些麻烦,不确定最好的方法
Objective = 从文件导入 csv,转换为 dict 列表,获取用户输入(数字),从键打印(数字)值。 csv 是 .txt 文件 .txt 文件中的输入示例为: (routes.txt)下面是在这个文件中,
route number, happy customers
123,4
321,0
789,6.7001
我可以使用以下命令将数据转换为字典列表,
number = input("please enter number of spare busses that can be assigned to routes: ")
if (os.path.exists("routes.txt")) == True:
with open("routes.txt", "r") as in_file:
list_dict = csv.DictReader(in_file,delimiter = ",")
list_dict = list(list_dict)
print(list_dict)
##### output = [{'Route number': '123', 'Happy customers': '4'}, {'Route number': '321', 'Happy customers': '0'}, {'Route number' : '789', 'Happy customers': '6.7001'}]
else:
print("The File routes.txt can not be found, please ensure the file is in the current working directory.")
但是我似乎找不到一种方法来获取(数字)打印出来的数字来自用户输入,并且需要打印该数字(数字)仅在列表排序后来自字典列表的路线编号,我发现唯一接近的是枚举到新的嵌套字典,但我不知道如何从中获取数据。任何指向正确方向的帮助将不胜感激。
i am having a bit of trouble with this not sure of best way to go
objective = import csv from file, convert to list of dict, get user input (number), print (number) of values from key. csv is a .txt file example input in .txt file is:
(routes.txt) below is in this file
route number, happy customers
123,4
321,0
789,6.7001
i can convert data to a list of dicts using the following
number = input("please enter number of spare busses that can be assigned to routes: ")
if (os.path.exists("routes.txt")) == True:
with open("routes.txt", "r") as in_file:
list_dict = csv.DictReader(in_file,delimiter = ",")
list_dict = list(list_dict)
print(list_dict)
##### output = [{'Route number': '123', 'Happy customers': '4'}, {'Route number': '321', 'Happy customers': '0'}, {'Route number' : '789', 'Happy customers': '6.7001'}]
else:
print("The File routes.txt can not be found, please ensure the file is in the current working directory.")
however i can't seem to find a way to get the (number) printed out number is from user input and need to print that (number) only of route numbers from list of dict after the list has been sorted only thing that i found comes close is enumerate to new nested dict but i dont know how to get data from that. any help pointing in right direction would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发现这可以将字典列表中的所有路线编号在排序后转换为路线编号列表。
found this to convert all route numbers in the list of dictionaries to a list of route numbers after they had been sorted.