[英]how to show specific columns from a list, tabulate and print them on the console
i am currently trying to do some password manager and i am stuck on how to elegantly print specific colums from a list.我目前正在尝试做一些密码管理器,我被困在如何优雅地打印列表中的特定列。 I could do this in a very dirty way but im sure there has to be a one-line way of managing it.
我可以以一种非常肮脏的方式做到这一点,但我确信必须有一种单线的方式来管理它。 The csv_file has 3 colums: "service", "account" and "password".
csv_file 有 3 个列:“服务”、“帐户”和“密码”。 I want to show only the firtst two colums that satisfies the user input in a tabulated way, to imitate a table.
我只想以表格的方式显示满足用户输入的前两列,以模仿表格。 Thank you in advanced for your time: ).
提前感谢您的时间:)。 This is how I tried to do it:
这就是我尝试这样做的方式:
reader = csv.DictReader(csv_file)
service = input("PLEASE SPECIFY THE SERVICE \n ")
header=[["service","account"]]
for line in reader:
if line["service"] != service:
reader.remove(line)
print(tabulate(([header, [el["service"], el["account"]] for el in reader]))
How about this?这个怎么样?
reader = csv.DictReader(csv_file)
service = input("PLEASE SPECIFY THE SERVICE \n ")
print(tabulate(line for line in reader if line["service"] == service,
header=["service","account"])
After a long investigation i found a way of doing it:经过长时间的调查,我找到了一种方法:
service = input("PLEASE SPECIFY THE SERVICE \n")
reader = csv.DictReader(csv_file)
header=["service","account"]
valid=[]
for line in reader:
if line["service"] != service:
valid.append([line["service"], line["account"], line["password"]])
print(tabulate([header, *[[line[0], line[1]] for line in valid]]))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.