23
23
Financial Telecommunication which is the official IBAN registrar) to get
24
24
the data needed to correctly parse and validate IBANs."""
25
25
26
+ import csv
26
27
import urllib
27
28
28
29
29
30
# The place where the current version of IBAN_Registry.txt can be downloaded.
30
31
download_url = 'http://www.swift.com/dsp/resources/documents/IBAN_Registry.txt'
31
32
32
33
33
- def splitlines (f ):
34
- """Read lines from the TAB-delimited IBAN_Registry.txt file and return
35
- a dictionary per read line. We clean up the values a bit because it
36
- contains some junk."""
34
+ def clean_row (row ):
35
+ """Clean up a read row from the CSV file."""
37
36
stripit = ' \t \n \r ;:\' "'
38
- firstline = [x .strip (stripit ) for x in f .readline ().lower ().split ('\t ' )]
39
- for line in f :
40
- yield dict (zip (firstline , [x .strip (stripit )
41
- for x in line .split ('\t ' )]))
37
+ return dict (
38
+ (k .strip (stripit ).lower (), v .strip (stripit ))
39
+ for k , v in row .items ())
42
40
43
41
44
42
def get_country_codes (line ):
@@ -54,11 +52,17 @@ def parse(f):
54
52
"""Parse the specified file."""
55
53
print '# generated from IBAN_Registry.txt, downloaded from'
56
54
print '# %s' % download_url
57
- for line in splitlines (f ):
58
- for cc in get_country_codes (line ):
55
+ for row in csv .DictReader (f , delimiter = '\t ' , quotechar = '"' ):
56
+ row = clean_row (row )
57
+ bban = row ['bban structure' ]
58
+ if not (bban ) or bban .lower () == 'not in use' :
59
+ bban = row ['iban structure' ]
60
+ for cc in get_country_codes (row ):
61
+ if bban .startswith (cc + '2!n' ):
62
+ bban = bban [5 :]
59
63
# print country line
60
64
print '%s country="%s" bban="%s"' % (
61
- cc , line ['name of country' ], line [ ' bban structure' ] )
65
+ cc , row ['name of country' ], bban )
62
66
# TODO: some countries have a fixed check digit value
63
67
# TODO: some countries have extra check digits
64
68
# TODO: use "Bank identifier position within the BBAN" field
0 commit comments