Skip to content

Commit 3dc6271

Browse files
Sebastien PonceSebastien Ponce
Sebastien Ponce
authored and
Sebastien Ponce
committed
Fixed csv printouts
In cases where one field contains special characters, especially commas, the output was incorrect. Now the field is double quoted and double quotes escaped as necessary
1 parent 81c8e04 commit 3dc6271

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

fstimer/printcsv.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@
2222

2323
import os
2424
import fstimer.printer
25+
import io, csv
26+
27+
def csv2string(data):
28+
si = io.StringIO()
29+
cw = csv.writer(si)
30+
cw.writerow(data)
31+
return si.getvalue().strip('\r\n')
2532

2633
class CSVPrinter(fstimer.printer.Printer):
2734
'''Printer class for csv files for single lap races'''
@@ -43,7 +50,7 @@ def file_extension(self):
4350

4451
def scratch_table_header(self):
4552
'''Returns the header of the printout for scratch results'''
46-
return 'Place,' + ','.join(self.fields) + '\n'
53+
return 'Place,' + csv2string(self.fields) + '\n'
4754

4855
def cat_table_header(self, category):
4956
'''Returns the header of the printout for results by category.
@@ -54,7 +61,7 @@ def cat_table_header(self, category):
5461
def common_entry(self, row):
5562
'''Returns the common part of the printout of the entry
5663
of a given runner for scratch or by category results'''
57-
return ','.join(row) + '\n'
64+
return csv2string(row) + '\n'
5865

5966
def scratch_entry(self, row):
6067
'''Returns the printout of the entry of a given runner

fstimer/printcsvlaps.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@
2222

2323
import fstimer.printcsv
2424
import os
25+
import io, csv
26+
27+
def csv2string(data):
28+
si = io.StringIO()
29+
cw = csv.writer(si)
30+
cw.writerow(data)
31+
return si.getvalue().strip('\r\n')
2532

2633
class CSVPrinterLaps(fstimer.printcsv.CSVPrinter):
2734
'''Printer class for csv files for multi lap races'''
@@ -43,11 +50,11 @@ def common_entry(self, row):
4350
idx_lap = self.fields.index('Lap Times')
4451
lap_times = row[idx_lap]
4552
row_print[idx_lap] = lap_times[0]
46-
entry = ','.join(row_print)+'\n'
53+
entry = csv2string(row_print)+'\n'
4754
if 'Lap Times' in self.fields:
4855
for i in range(1, len(lap_times)):
4956
entry += ',' # for Place
5057
row_print = ['' for j in range(len(row))]
5158
row_print[idx_lap] = str(lap_times[i])
52-
entry += ','.join(row_print) + '\n'
53-
return entry
59+
entry += csv2string(row_print) + '\n'
60+
return entry

0 commit comments

Comments
 (0)