Here is what I have so far: import os import csv
countries = {} reader = csv.reader(open("countries.csv")) for row in reader: code, name = row countries[name] = code files = set([file for file in os.listdir(os.getcwd()) if file.endswith('svg')]) print len(files) for file in files: file = file.strip('.svg') print file # if countries.has_key(file): # print file When I run this I get: Flag_of_Uganda Flag_of_the_United_State Flag_of_Abkhazia Flag_of_Montenegro Flag_of_Qatar Flag_of_Gabon Flag_of_Uzbekistan Flag_of_Kiribati Flag_of_Armenia Flag_of_Panama Flag_of_Monaco Flag_of_Australia Flag_of_Liechtenstein Flag_of_Tunisia Flag_of_Georgia Flag_of_Palau Flag_of_the_Central_African_Republic ... The problem is that for example the file Flag_of_the_United_States.svg when I use the strip('.svg') it is returned as Flag_of_the_United_State Also, How do I remove 'Flag_of', 'Flag_of_the_' I guess after this I can compare the value with the key and map the tld? Or is it better to use regex and then search from the list of countries? But how??? -------- Original Message -------- From: Tim Golden <m...@timgolden.me.uk> Apparently from: tutor-bounces+davidwilson=safe-mail....@python.org To: Cc: tutor@python.org Subject: Re: [Tutor] renaming files within a directory Date: Sun, 26 Jul 2009 20:41:10 +0100 > davidwil...@safe-mail.net wrote: > > OK I am lost ;( > > > > I changed the code to: > > > >>>> reader = csv.reader(open("countries.csv"), delimiter=";") > >>>> for row in reader: > > ... print row > > ... > > ['bi', 'Burundi'] > > ['km', 'Comoros'] > > ['dj', 'Djibouti'] > > ['er', 'Eritrea'] > > > > ... > > > > Now each row is a list with two items each. > > > > But when I do this: > > > >>>> dic = [] > >>>> for row in reader: > > ... newdic.append({row[0]: row[1]}) > > ... > >>>> dic > > [] > > > > I get an empty dictionary > > Well, you actually get an empty list :) > To instantiate an empty dictionary, you use curly brackets: > > d = {} > > To add something to a dictionary, you use: > > d[<key>] = <value> > > Try something like this: > > <code - untested> > import csv > > reader = csv.reader(open("countries.csv"), delimiter=";") > countries = {} # note the curly brackets > for row in reader: > code, name = row # handy Python tuple unpacking > countries[name] = code > > </code> > > > Once you're used to the idea, you can get reasonably slick > with dictionary initialisers and generator expressions: > > import csv > > reader = csv.reader(open("countries.csv"), delimiter=";") > countries = dict ((row[1], row[0]) for row in reader) > > And once you're really confident (and if you're a > fan of one-liners) you can get away with this: > > import csv > countries = dict ( > (name, code) for \ > (code, name) in \ > csv.reader (open ("countries.csv"), delimiter=";") > ) > > > BTW, I tend to open csv files with "rb" as it seems to > avoid line-ending issues with the csv module. YMMV. > > TJG > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor