[Tutor] create dictionary from csv data

Norman Khine norman at khine.net
Mon Feb 23 14:41:10 CET 2009


Hello,

I have this csv file:

$ cat licences.csv
"1","Air Travel Organisation Licence (ATOL)\n Operates Inclusive Tours (IT)"
"2","Air Travel Organisation Licence (ATOL)\n Appointed Agents of IATA 
(IATA)"
"3", "Association of British Travel Agents (ABTA) No. 56542\n Air Travel
Organisation Licence (ATOL)\n Appointed Agents of IATA (IATA)\n 
Incentive Travel & Meet. Association (ITMA)"

I would like to create a set of unique values for all the memberships. i.e.

ATOL
IT
ABTA
etc..

and also I would like to extract the No. 56542

and lastly I would like to map each record to the set of unique 
membership values, so that:

I have a dictionary like:

{0: ['1', '('ATOL', 'IT')'],
1: ['2','('ATOL', 'IATA')'],
2: ['3','('ABTA', 'ATOL', 'IATA', 'ITMA')']}

Here is what I have so far:

 >>> import csv
 >>> inputFile = open(str("licences.csv"),  'r')
 >>> outputDic = {}
 >>> keyIndex = 0
 >>> fileReader = csv.reader(inputFile)
 >>> for line in fileReader:
...     outputDic[keyIndex] = line
...     keyIndex+=1
...
 >>> print outputDic
{0: ['2', 'Air Travel Organisation Licence (ATOL) Appointed Agents of 
IATA (IATA)'], 1: ['3', ' "Association of British Travel Agents (ABTA) 
No. 56542 Air Travel'], 2: ['Organisation Licence (ATOL) Appointed 
Agents of IATA (IATA) Incentive Travel & Meet. Association (ITMA)"']}

So basically I would like to keep only the data in the brackets, i.e. 
(ABTA) etc..

Cheers

Norman


More information about the Tutor mailing list