gpt4 book ai didi

python - 无法用python读取字典文件中输入的内容

转载 作者:太空宇宙 更新时间:2023-11-03 16:16:50 24 4
gpt4 key购买 nike

我尝试制作一个Python字典,文件格式(.txt或.csv)的输入,并且我想匹配我拥有的基于数据库。

这是我的文件输入

John
Smith
Ana
Adam
Steven

这是我的脚本

data = [{"name":"John","school":"A","age":"17"},
{"name":"Smith","school":"B ","age":"16"},
{"name":"Ana","school":"B","age":"19 "},
{"name":"Adam","school":"C ","age":"18 "},
{"name":"Steven","school":"B ","age":"19 "},]

file_input = open ('/home/ubuntu/data.txt', 'r')
for line in file_input:
for get_name in data:
if get_name ["name"] == line:
print "Name :", get_name ['name'],
print "School :", get_name ['school'],
print "Age :", get_name ['age']
else
print ("No found name")

运行结果后

python myscript.py
No found name

最佳答案

将 get_name 更改为 get_school

data = [{"name":"John","school":"A","age":"17"},
{"name":"Smith","school":"B ","age":"16"},
{"name":"Ana","school":"B","age":"19 "},
{"name":"Adam","school":"C ","age":"18 "},
{"name":"Steven","school":"B ","age":"19 "},]

file_input = open ('/home/ubuntu/data.txt', 'r')
for line in file_input:
for get_school in data:
if get_school["name"] == line.replace('\n','')::
print "Name :", get_school['name'],
print "School :", get_school['school'],
print "Age :", get_school['age']
else:
print ("No found name")

关于python - 无法用python读取字典文件中输入的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38878281/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com