From 44f3025cc6eb405f8271fec5e03becd41e2fcfd1 Mon Sep 17 00:00:00 2001 From: Jonathan Harris Date: Fri, 17 Nov 2017 18:10:39 +0000 Subject: [PATCH] Handle non-ASCII Cmdr and ship names in command-line output --- edshipyard.py | 2 +- stats.py | 15 +++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/edshipyard.py b/edshipyard.py index 817aa31a..8274ab51 100644 --- a/edshipyard.py +++ b/edshipyard.py @@ -118,7 +118,7 @@ def export(data, filename=None): # Construct description ship = ship_map.get(data['ship']['name'].lower(), data['ship']['name']) - string = '[%s]\n' % (data['ship'].get('shipName') and ', '.join([ship, data['ship']['shipName']]) or ship) + string = '[%s]\n' % (data['ship'].get('shipName') and ', '.join([ship, data['ship']['shipName'].encode('utf-8')]) or ship) for slot in ['H', 'L', 'M', 'S', 'U', None, 'BH', 'RB', 'TM', 'FH', 'EC', 'PC', 'SS', 'FS', None, 'MC', None, '9', '8', '7', '6', '5', '4', '3', '2', '1']: if not slot: string += '\n' diff --git a/stats.py b/stats.py index dd006f24..ca624aa7 100644 --- a/stats.py +++ b/stats.py @@ -1,4 +1,5 @@ from collections import OrderedDict +import csv from sys import platform from functools import partial import time @@ -141,11 +142,10 @@ def status(data): def export_status(data, filename): - h = open(filename, 'wt') - h.write('Category,Value\n') + h = csv.writer(open(filename, 'wb')) + h.writerow(['Category', 'Value']) for thing in status(data): - h.write(','.join(thing) + '\n') - h.close() + h.writerow([x.encode('utf-8') for x in thing]) # Returns id,name,shipName,system,station,value @@ -165,11 +165,10 @@ def ships(data): return [ (str(ship['id']), ship_map.get(ship['name'].lower(), ship['name']), ship.get('shipName', ''), ship['starsystem']['name'], ship['station']['name'], str(ship['value']['total'])) for ship in ships if ship] def export_ships(data, filename): - h = open(filename, 'wt') - h.write('Id,Ship,Name,System,Station,Value\n') + h = csv.writer(open(filename, 'wb')) + h.writerow(['Id', 'Ship', 'Name', 'System', 'Station', 'Value']) for thing in ships(data): - h.write(','.join(thing) + '\n') - h.close() + h.writerow([x.encode('utf-8') for x in thing]) class StatsDialog():