Home  >  Q&A  >  body text

python matching data output data

I now have a list with the content re=['test1','test2','test3'], and a csv or xlsx or txt with two columns of data

The effect I want to achieve is that if the element of re is equal to column A in the file, then the row of data in column AB of the file will be output. How to implement this specifically?

扔个三星炸死你扔个三星炸死你2686 days ago746

reply all(2)I'll reply

  • 黄舟

    黄舟2017-06-12 09:26:22

    You need to read the file first, then put the result into the array
    and then search the value of the array

    reply
    0
  • 欧阳克

    欧阳克2017-06-12 09:26:22

    import csv
    with open("lookup.csv") as f:
        reader = csv.reader(f, delimiter=',')
        dict_lookup = {r[0]:r[1] for r in reader}
    print(dict_lookup) 
    print(dict_lookup['test2'])

    Output

    {'test1': 'output1', 'test2': 'output2', 'test3': 'output3'}
    output2

    If the content of lookup.csv is

    test1,output1
    test2,output2
    test3,output3

    This is a very basic operation of using the csv module, it is recommended to take a look

    reply
    0
  • Cancelreply