8000 Update getiban script · sharoonthomas/python-stdnum@6c49ca8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6c49ca8

Browse files
committed
Update getiban script
This switches to use the csv module to support multi-line column values. This also handles some problems in the BBAN structure column that would contain an IBAN structure.
1 parent 0ee74e5 commit 6c49ca8

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

getiban.py

Lines changed: 1 10000 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,20 @@
2323
Financial Telecommunication which is the official IBAN registrar) to get
2424
the data needed to correctly parse and validate IBANs."""
2525

26+
import csv
2627
import urllib
2728

2829

2930
# The place where the current version of IBAN_Registry.txt can be downloaded.
3031
download_url = 'http://www.swift.com/dsp/resources/documents/IBAN_Registry.txt'
3132

3233

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."""
3736
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())
4240

4341

4442
def get_country_codes(line):
@@ -54,11 +52,17 @@ def parse(f):
5452
"""Parse the specified file."""
5553
print '# generated from IBAN_Registry.txt, downloaded from'
5654
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:]
5963
# print country line
6064
print '%s country="%s" bban="%s"' % (
61-
cc, line['name of country'], line['bban structure'])
65+
cc, row['name of country'], bban)
6266
# TODO: some countries have a fixed check digit value
6367
# TODO: some countries have extra check digits
6468
# TODO: use "Bank identifier position within the BBAN" field

0 commit comments

Comments
 (0)
0