Python 2.7: Returning a value in a csv file from input
Question: I've got a csv with:

    
    
    T,8,101
    T,10,102
    T,5,103
    

and need to search the csv file, in the 3rd column for my input, and if found,
return the 2nd column value in that same row (searching "102" would return
"10"). I then need to save the result to use in another calculation. (I am
just trying to print the result for now..) I am new to python (2 weeks) and
wanted to get a grasp on reading/writing in csv files. All the searchable
results, didn't give me the answer I needed. Thanks

Here is my code:

    
    
    name = input("waiting")
    import csv
    with open('cards.csv', 'rt') as csvfile:
        reader = csv.reader(csvfile, delimiter=',')
        for row in reader:
            if row[2] == name:
                print(row[1])
    


Answer: As stated in my comment above I would implement a general approach without
using the csv-module like this:

    
    
    import io
    
    s = """T,8,101
    T,10,102
    T,5,103"""
    
    # use io.StringIO to get a file-like object
    f = io.StringIO(s)
    lines = [tuple(line.split(',')) for line in f.read().splitlines()]
    
    example_look_up = '101'
    
    def find_element(look_up):
        for t in lines:
            if t[2] == look_up:
                return t[1]
    
    result = find_element(example_look_up)
    print(result)
    

Please keep in mind, that this is Python3-code. You need to replace `print()`
with `print` if using with Python2 and maybe change something related to the
`StringIO` which I am using for demonstration purposes here in order to get a
file-like object. However, this snippet should give you a basic idea about a
possible solution.

