0

I am trying to implement a function that takes in a filename and a list of column names and returns a list of integers that contains the values in those columns from every row. Below is my code

def extract_election_votes(filename, column_names):

    election = open(filename, encoding="iso-8859-1")
    input_file = csv.DictReader(election)
    results = []
    # store vote counts
    for row in input_file:
        for col in column_names:
            x = row[col].replace(",", "")
            x = row[col].replace("", '')
            # replacing commas/quotes w empty string
            if row[col] == " ":
                continue
            # ignores cells w no data
            votes = int(x)
            # convert string to integer
            results.append(votes)
    election.close()
    return results

I am getting an error with the line votes = int(x) that says ValueError: invalid literal for int() with base 10: '1,131,111'. How can I fix my code so that this error does not persist?

Here is my assertion:

ir_data = extract_election_votes("election-iran-2009.csv", ["Ahmadinejad", "Rezai", "Karrubi", "Mousavi"])                                                              

Here is the data for reference: ElectionDataFile

Thanks!

1 Answers1

0

You're overwriting the old value of x with the second replace, so the commas come back. The second replace should be x.replace(...) instead of row[col].replace(...).

karx
  • 11
  • 3