3

So I've just started with python the last few days with nothing but some basic coding knowledge from 5 years ago. I'm in love.

I'm importing a .csv which contains historical alliances sorted by dyad...so each alliance has a special number. There are many more in the file with the same country. I'm trying to have a user give a country and a date and be able to return the countries they were in alliance with that year. Here's a row of data.

6 200 United Kingdom 255 Germany 1 1 1816 31 10 1822 1 0 1 0 0  1 0
6 200 United Kingdom 300 Austria-Hungary 1 1 1816 31 10 1822 1 0 1 0 0 1 0

First number is the dyad num, then country code and country, country code and country, day of alliance beginning, month, year, day of end, month, year. Last parts aren't really important.

import csv
name='United Kingdom'
diad = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
with open('list.csv') as inputfile:
    r = list(csv.reader(inputfile))
    for i, j in enumerate(r):
         if j == name:
              diad[0]=name
          print "true"
              diad.insert(0, name)

I run it and get nothing, so the if statement isn't true... I don't understand how though. You're looking at the first two lines of data, shouldn't enumerate find any instance of "United Kingdom" and insert what it found into the diad list? If I can make this piece work my task should be easy.

kojiro
  • 74,557
  • 19
  • 143
  • 201
kodiak3
  • 45
  • 5
  • 5
    Python uses whitespace to denote code blocks. Be sure you're consistent. It's generally preferred to use four spaces as an indent, and never use tabs. It looks like you're using a tab on the `print` line – mhlester Mar 10 '14 at 20:43
  • Not sure why it came out like that I'll edit it. – kodiak3 Mar 10 '14 at 20:49
  • If I'm reading this right... r is an entire line. you want `enumerate(r.split())`... – Corley Brigman Mar 10 '14 at 20:50

1 Answers1

2

Iterating through a CSV iterates through the rows, not the columns. So in each iteration, j is a list of columns. You'd then need to iterate through the elements in j as well, in order to compare against your string.

I think you're confused about what enumerate does. You don't need it here at all, in fact. What it does is give you an index along with the value of the thing you're enumerating. So in this case, you'd get on the first iteration 0 plus the first row, then 1 with the second row, and so on. Since you're not using the index, you don't want to use enumerate.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Since the .csv is structured, you could probably just check for `name` in the specific "country" indexes (looks like 2 and 4 here) instead of checking all the columns in the row. And if the csv had headers, then the two fields could be searched by their header name instead of index. – crennie Mar 10 '14 at 21:05
  • This was a bad post of code. It was testcode or something, I can't explain it. But thanks, I just realized that j[0] would return 6, 6 for the data above. Basically I'm naming each country as a class manually, setting a name attribute...like United_Kingdom = country("United_Kingdom") – kodiak3 Mar 10 '14 at 21:07
  • and then(within __init__ in the class) iterating through the imported list for a collision with the name, then adding the dyad num to a list of dyads for that country... But this answer helped, Thanks. Hopefully better questions in the future. – kodiak3 Mar 10 '14 at 21:16
  • In case anyone reads this...http://stackoverflow.com/questions/5466618/too-many-values-to-unpack-iterating-over-a-dict-key-string-value-list That helped with this too. – kodiak3 Mar 10 '14 at 22:38