From cf467ce1245ac47f8bea4691c9574ed9a7fe8ff1 Mon Sep 17 00:00:00 2001 From: emkael Date: Tue, 21 May 2019 23:24:28 +0200 Subject: CSV format support --- dealconvert/formats/csv.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'dealconvert') diff --git a/dealconvert/formats/csv.py b/dealconvert/formats/csv.py index 928d62e..d0d8b28 100644 --- a/dealconvert/formats/csv.py +++ b/dealconvert/formats/csv.py @@ -1,6 +1,41 @@ +from __future__ import absolute_import +import csv + from . import DealFormat +from .. import dto class CSVFormat(DealFormat): @property def suffix(self): return '.csv' + + def parse_content(self, content): + dealset = [] + for line in csv.reader(content): + deal = dto.Deal() + deal.number = int(line[16]) + dealers = {'N': dto.POSITION_NORTH, + 'E': dto.POSITION_EAST, + 'S': dto.POSITION_SOUTH, + 'W': dto.POSITION_WEST} + deal.dealer = dealers[line[17].split('/')[0]] + for pair in ['NS', 'EW']: + deal.vulnerable[pair] = line[17].split('/')[1] in [pair, 'All'] + for hand in range(0, 4): + for suit in range(0, 4): + deal.hands[hand][suit] = list(line[hand*4+suit]) + dealset.append(deal) + return dealset + + def output_content(self, out_file, dealset): + writer = csv.writer(out_file, quoting=csv.QUOTE_ALL) + for deal in dealset: + line = [] + for hand in deal.hands: + line += [''.join(suit) for suit in hand] + line += [str(deal.number), ''] + line[17] = 'NESW'[deal.dealer] + '/' + line[17] += ('All' if deal.vulnerable['EW'] else 'NS') \ + if deal.vulnerable['NS'] \ + else ('EW' if deal.vulnerable['EW'] else '-') + writer.writerow(line) -- cgit v1.2.3