这是一个中等复杂度的解析、处理和格式化任务,所以我会使用类:一个用于道路一侧的连续地址范围,一个用于该范围的容器。这样加载界面就像添加一个解码的行一样简单,格式化界面也一样简单。
代码可以是:
# set the filenames here:
filename = "input.csv"
out = output.txt
class AddressRange:
def __init__(self, row):
self.road = row[0]
self.low = self.high = int(row[1])
self.side = row[2]
def add(self, row):
if (self.road != row[0]) or (self.side != row[2]):
return False
val = int(row[1])
if val < self.low <= val+2:
self.low = val
elif val-2 <= self.high < val:
self.high = val
else:
return False
return True
def __str__(self):
return '%s %d-%d' % (self.road, self.low, self.high)
class AddressList:
def __init__(self):
self.ranges = []
def add(self, row):
for ar in self.ranges:
if ar.add(row):
break
else:
self.ranges.append(AddressRange(row))
def print(self, fd=sys.stdout):
for ar in self.ranges:
print(str(ar), file=fd)
with open(filename) as fd:
rd = csv.reader(fd, delimiter = ';')
lst = AddressList()
_ = next(fd) # skip header line
for row in rd:
if len(row) == 3:
lst.add(row)
with open(out, 'w') as fd:
lst.print(out)
使用示例输入文件,输出如预期:
Northroad 1-3
Northroad 2-4
Northroad 8-10
Southroad 1-3
Southroad 2-4
Southroad 10-14