i've got csv with:
t,8,101 t,10,102 t,5,103
and need search csv file, in 3rd column input, , if found, return 2nd column value in same row (searching "102" return "10"). need save result use in calculation. (i trying print result now..) new python (2 weeks) , wanted grasp on reading/writing in csv files. searchable results, didn't give me answer needed. thanks
here code:
name = input("waiting") import csv open('cards.csv', 'rt') csvfile: reader = csv.reader(csvfile, delimiter=',') row in reader: if row[2] == name: print(row[1])
as stated in comment above implement general approach without using csv-module this:
import io s = """t,8,101 t,10,102 t,5,103""" # use io.stringio file-like object f = io.stringio(s) lines = [tuple(line.split(',')) line in f.read().splitlines()] example_look_up = '101' def find_element(look_up): t in lines: if t[2] == look_up: return t[1] result = find_element(example_look_up) print(result)
please keep in mind, python3-code. need replace print()
print
if using python2 , maybe change related stringio
using demonstration purposes here in order file-like object. however, snippet should give basic idea possible solution.
Comments
Post a Comment