Skip to content Skip to sidebar Skip to footer

Search A Single Column For A Particular Value In A Csv File And Return An Entire Row

Issue The code does not correctly identify the input (item). It simply dumps to my failure message even if such a value exists in the CSV file. Can anyone help me determine what I

Solution 1:

You have three problems with this:

  • You return on the first failure, so it will never get past the first line.
  • You are reading strings from the file, and comparing to an int.
  • _make iterates over the dictionary keys, not the values, producing the wrong result (item_data(Item='Name', Name='Price', Cost='Qty', Qty='Item', Price='Cost', Description='Description')).

    for row in (item_data(**data) fordatain read_data):
        if row.Item == str(item):
            return row
    return failure
    

This fixes the issues at hand - we check against a string, and we only return if none of the items matched (although you might want to begin converting the strings to ints in the data rather than this hackish fix for the string/int issue).

I have also changed the way you are looping - using a generator expression makes for a more natural syntax, using the normal construction syntax for named attributes from a dict. This is cleaner and more readable than using _make and map(). It also fixes problem 3.

Post a Comment for "Search A Single Column For A Particular Value In A Csv File And Return An Entire Row"