Skip to content
Advertisement

search and find from csv with dict

I have a csv file containing info like this:

JavaScript

I also have a dictionary in python containing similar info :

JavaScript

how can I search values of dict in my csv file and print matching row in CSV to keys in dictionary in out put for example :

JavaScript

Advertisement

Answer

Here’s an idea:

  1. Create another, inverted dictionary which maps your values to their keys:
JavaScript
  1. Open your csv file, iterate the lines of it, and extract the first and second elements of each line:
JavaScript
  1. Check if the second element is in your inverted dict, and if so, print the appropriate mapping:
JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement