0

I'm trying to figure out how to take the csv file I opened and convert specific columns to ints since when the csv is opened and read, every element becomes an int.

Inside of the for row in csv_list: loop, I was able to append row to the people_dna list. I know that I can wrap row[index] inside of an int() function, but I'm not sure how to skip the first column with string values. Is there a way to do this without having to manually enter each index of the list? Here's my code:

 Program should open the .csv file and read its contents into memory
# Read .csv file
with open(argv[1], mode='r') as csv_file:
    # Convert the .csv to a DICTIONARY and save to memory
    csv_list = csv.reader(csv_file)
    # Create empty list for each row of .csv
    people_dna = []
    for row in csv_list:
        # Push each row into the list (array)
        people_dna.append(row)

   # Remove 'name' from first row of .csv
    people_dna[0].remove('name')

    # Save the first row, genome names from the csv file to a variable
    genomes_row = people_dna[0]
    print(genomes_row)

    # Remove the genomes row from the people_dna list
    people_dna.pop(0)

    for item in people_dna:
        # Print each row the .csv dna list after genomes row is removed
        print(item)

Here's the output of the csv file:

['AGATC', 'TTTTTTCT', 'AATG', 'TCTAG', 'GATA', 'TATC', 'GAAA', 'TCTG']
['Albus', '15', '49', '38', '5', '14', '44', '14', '12']
['Cedric', '31', '21', '41', '28', '30', '9', '36', '44']
['Draco', '9', '13', '8', '26', '15', '25', '41', '39']
['Fred', '37', '40', '10', '6', '5', '10', '28', '8']
['Ginny', '37', '47', '10', '23', '5', '48', '28', '23']
['Hagrid', '25', '38', '45', '49', '39', '18', '42', '30']
['Harry', '46', '49', '48', '29', '15', '5', '28', '40']
['Hermione', '43', '31', '18', '25', '26', '47', '31', '36']
['James', '46', '41', '38', '29', '15', '5', '48', '22']
['Kingsley', '7', '11', '18', '33', '39', '31', '23', '14']
['Lavender', '22', '33', '43', '12', '26', '18', '47', '41']
['Lily', '42', '47', '48', '18', '35', '46', '48', '50']
['Lucius', '9', '13', '33', '26', '45', '11', '36', '39']
['Luna', '18', '23', '35', '13', '11', '19', '14', '24']
['Minerva', '17', '49', '18', '7', '6', '18', '17', '30']
['Neville', '14', '44', '28', '27', '19', '7', '25', '20']
['Petunia', '29', '29', '40', '31', '45', '20', '40', '35']
['Remus', '6', '18', '5', '42', '39', '28', '44', '22']
['Ron', '37', '47', '13', '25', '17', '6', '13', '35']
['Severus', '29', '27', '32', '41', '6', '27', '8', '34']
['Sirius', '31', '11', '28', '26', '35', '19', '33', '6']
['Vernon', '26', '45', '34', '50', '44', '30', '32', '28']

I actually wanted to skip the first row entirely, so I popped it from the list with people_dna.pop(0), so the final output of the csv actually starts with 'Albus'. What I'd like to know is how to convert everything in people_dna, except for the names in the first column, to an int so I can later compare the item list

1

Without answering your question directly (i.e. how to convert to int), let me point out that your csv_list is not actually a list. If you run a print(type(csv_list)) as a sanity check, you'll see it shows <class '_csv.reader'>. To read in the data, change your code to the following:

with open(argv[1], mode='r') as csv_file:
    csv_list = list(csv.reader(csv_file))

Now you can easily subset without jumping through hoops. For example, if you want to print the first 5 rows without the header row:

for i in range(1, 6):
    print(csv_list[i])

Output:

['Albus', '15', '49', '38', '5', '14', '44', '14', '12']
['Cedric', '31', '21', '41', '28', '30', '9', '36', '44']
['Draco', '9', '13', '8', '26', '15', '25', '41', '39']
['Fred', '37', '40', '10', '6', '5', '10', '28', '8']
['Ginny', '37', '47', '10', '23', '5', '48', '28', '23']

You can manipulate along columns in the same way.

| improve this answer | |
  • Right, thank you. I used for i in range(len(csv_list)): and that worked as well. I didn't realize that I wasn't really creating a list. – Ang Jun 7 at 17:47
  • No worries - it's a common tripper. Glad it worked out. Do please mark the response as correct for later readers, btw. – user3671 Jun 8 at 10:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .