mirror of
https://github.com/Hopiu/linkchecker.git
synced 2026-04-29 18:44:43 +00:00
Python3: fix csvlog
This commit is contained in:
parent
5179e47c52
commit
55a7973b93
1 changed files with 16 additions and 4 deletions
|
|
@ -19,7 +19,10 @@ A CSV logger.
|
||||||
"""
|
"""
|
||||||
import csv
|
import csv
|
||||||
import os
|
import os
|
||||||
from io import BytesIO
|
try:
|
||||||
|
from cStringIO import StringIO
|
||||||
|
except ImportError: # Python 3
|
||||||
|
from io import StringIO
|
||||||
from . import _Logger
|
from . import _Logger
|
||||||
from .. import strformat
|
from .. import strformat
|
||||||
|
|
||||||
|
|
@ -69,7 +72,7 @@ class CSVLogger (_Logger):
|
||||||
else:
|
else:
|
||||||
# write empty string to initialize file output
|
# write empty string to initialize file output
|
||||||
self.write(u"")
|
self.write(u"")
|
||||||
self.queue = BytesIO()
|
self.queue = StringIO()
|
||||||
self.writer = csv.writer(self.queue, dialect=self.dialect,
|
self.writer = csv.writer(self.queue, dialect=self.dialect,
|
||||||
delimiter=self.separator, lineterminator=self.linesep,
|
delimiter=self.separator, lineterminator=self.linesep,
|
||||||
quotechar=self.quotechar)
|
quotechar=self.quotechar)
|
||||||
|
|
@ -119,12 +122,21 @@ class CSVLogger (_Logger):
|
||||||
self.writerow(map(strformat.unicode_safe, row))
|
self.writerow(map(strformat.unicode_safe, row))
|
||||||
self.flush()
|
self.flush()
|
||||||
|
|
||||||
|
def encode_row_s(self, row_s):
|
||||||
|
if isinstance(row_s, str):
|
||||||
|
return row_s # Python 3
|
||||||
|
else:
|
||||||
|
return row_s.encode("utf-8", self.codec_errors) # Python 2
|
||||||
|
|
||||||
def writerow (self, row):
|
def writerow (self, row):
|
||||||
"""Write one row in CSV format."""
|
"""Write one row in CSV format."""
|
||||||
self.writer.writerow([s.encode("utf-8", self.codec_errors) for s in row])
|
self.writer.writerow([self.encode_row_s(s) for s in row])
|
||||||
# Fetch UTF-8 output from the queue ...
|
# Fetch UTF-8 output from the queue ...
|
||||||
data = self.queue.getvalue()
|
data = self.queue.getvalue()
|
||||||
data = data.decode("utf-8")
|
try:
|
||||||
|
data = data.decode("utf-8")
|
||||||
|
except AttributeError:
|
||||||
|
pass
|
||||||
# ... and write to the target stream
|
# ... and write to the target stream
|
||||||
self.write(data)
|
self.write(data)
|
||||||
# empty queue
|
# empty queue
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue