dwiyanto dwiyanto - 3 months ago 8
Python Question

Can not read the content input in the dictionary file with python

I tried to make a python dictionary, the input of file format (.txt or .csv) and I want to match based database that I have.


This my file input


John

Smith

Ana

Adam

Steven


This My script

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")


after I run the results

python myscript.py
No found name

Answer

change get_name to 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")
Comments