Skip to main content
deleted 44 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Python: getting Getting lists of values from a CSV - Is there a cleaner way to do this?

So, I have a function that takes a column title, and a response.body from a urllib GET (I already know the body contains text/csv), and iterates through the data to build a list of values to be returned. My question to the gurus here: have I written this in the cleanest, most efficient way possible? Can you suggest any improvements?

 
    def _get_values_from_csv(self, column_title, response_body):
        """retrieves"""retrieves specified values found in the csv body returned from GET
        @requires: csv
        @param column_title: the name of the column for which we'll build a list of return values.
        @param response_body: the raw GET output, which should contain the csv data
        @return: list of elements from the column specified.
        @note: the return values have duplicates removed. This could pose a problem, if you are looking for duplicates.
        I'm not sure how to deal with that issue.""""""
        dicts = [row for row in csv.DictReader(response_body.split("\r\n""\r\n"))]
        results = {}
        for dic in dicts:
            for k, v in dic.iteritems():
                try:
                    results[k] = results[k] + [v] #adds elements as list+list
                except: #first time through the iteritems loop.
                    results[k] = [v]

        #one potential problem with this technique: handling duplicate rows
        #not sure what to do about it.
        return_list = list(set(results[column_title]))
        return_list.sort()
        return return_list

Thanks!

Python: getting lists of values from a CSV - Is there a cleaner way to do this?

So, I have a function that takes a column title, and a response.body from a urllib GET (I already know the body contains text/csv), and iterates through the data to build a list of values to be returned. My question to the gurus here: have I written this in the cleanest, most efficient way possible? Can you suggest any improvements?

 
    def _get_values_from_csv(self, column_title, response_body):
        """retrieves specified values found in the csv body returned from GET
        @requires: csv
        @param column_title: the name of the column for which we'll build a list of return values.
        @param response_body: the raw GET output, which should contain the csv data
        @return: list of elements from the column specified.
        @note: the return values have duplicates removed. This could pose a problem, if you are looking for duplicates.
        I'm not sure how to deal with that issue."""
        dicts = [row for row in csv.DictReader(response_body.split("\r\n"))]
        results = {}
        for dic in dicts:
            for k, v in dic.iteritems():
                try:
                    results[k] = results[k] + [v] #adds elements as list+list
                except: #first time through the iteritems loop.
                    results[k] = [v]

        #one potential problem with this technique: handling duplicate rows
        #not sure what to do about it.
        return_list = list(set(results[column_title]))
        return_list.sort()
        return return_list

Thanks!

Getting lists of values from a CSV

I have a function that takes a column title, and a response.body from a urllib GET (I already know the body contains text/csv), and iterates through the data to build a list of values to be returned. My question to the gurus here: have I written this in the cleanest, most efficient way possible? Can you suggest any improvements?

def _get_values_from_csv(self, column_title, response_body):
    """retrieves specified values found in the csv body returned from GET
    @requires: csv
    @param column_title: the name of the column for which we'll build a list of return values.
    @param response_body: the raw GET output, which should contain the csv data
    @return: list of elements from the column specified.
    @note: the return values have duplicates removed. This could pose a problem, if you are looking for duplicates.
    I'm not sure how to deal with that issue."""
    dicts = [row for row in csv.DictReader(response_body.split("\r\n"))]
    results = {}
    for dic in dicts:
        for k, v in dic.iteritems():
            try:
                results[k] = results[k] + [v] #adds elements as list+list
            except: #first time through the iteritems loop.
                results[k] = [v]

    #one potential problem with this technique: handling duplicate rows
    #not sure what to do about it.
    return_list = list(set(results[column_title]))
    return_list.sort()
    return return_list
Tweeted twitter.com/#!/StackCodeReview/status/38249474224697344
Post Migrated Here from stackoverflow.com (revisions)
Source Link
Greg Gauthier
Greg Gauthier

Python: getting lists of values from a CSV - Is there a cleaner way to do this?

So, I have a function that takes a column title, and a response.body from a urllib GET (I already know the body contains text/csv), and iterates through the data to build a list of values to be returned. My question to the gurus here: have I written this in the cleanest, most efficient way possible? Can you suggest any improvements?

 
    def _get_values_from_csv(self, column_title, response_body):
        """retrieves specified values found in the csv body returned from GET
        @requires: csv
        @param column_title: the name of the column for which we'll build a list of return values.
        @param response_body: the raw GET output, which should contain the csv data
        @return: list of elements from the column specified.
        @note: the return values have duplicates removed. This could pose a problem, if you are looking for duplicates.
        I'm not sure how to deal with that issue."""
        dicts = [row for row in csv.DictReader(response_body.split("\r\n"))]
        results = {}
        for dic in dicts:
            for k, v in dic.iteritems():
                try:
                    results[k] = results[k] + [v] #adds elements as list+list
                except: #first time through the iteritems loop.
                    results[k] = [v]

        #one potential problem with this technique: handling duplicate rows
        #not sure what to do about it.
        return_list = list(set(results[column_title]))
        return_list.sort()
        return return_list

Thanks!