Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

csv_utils.py 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # -*- coding: utf-8 -*-
  2. import codecs
  3. import csv
  4. import six
  5. from warnings import warn
  6. from six.moves import cStringIO
  7. # These classes are copied from http://docs.python.org/2/library/csv.html
  8. class CsvUnicodeWriter(object):
  9. """
  10. A CSV writer which will write rows to CSV file "f",
  11. which is encoded in the given encoding.
  12. """
  13. def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
  14. # Redirect output to a queue
  15. self.queue = cStringIO()
  16. self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
  17. self.stream = f
  18. self.encoder = codecs.getincrementalencoder(encoding)()
  19. def cast_to_bytes(self, obj):
  20. if isinstance(obj, six.text_type):
  21. return obj.encode('utf-8')
  22. elif isinstance(obj, six.binary_type):
  23. return obj
  24. elif hasattr(obj, '__unicode__'):
  25. return six.text_type(obj).encode('utf-8')
  26. elif hasattr(obj, '__str__'):
  27. return str(obj)
  28. else:
  29. raise TypeError('Expecting unicode, str, or object castable'
  30. ' to unicode or string, got: %r' % type(obj))
  31. def cast_to_str(self, obj):
  32. warn('cast_to_str deprecated, please use cast_to_bytes instead')
  33. return self.cast_to_bytes(obj)
  34. def writerow(self, row):
  35. self.writer.writerow([self.cast_to_bytes(s) for s in row])
  36. # Fetch UTF-8 output from the queue ...
  37. data = self.queue.getvalue()
  38. data = data.decode("utf-8")
  39. # ... and reencode it into the target encoding
  40. data = self.encoder.encode(data)
  41. # write to the target stream
  42. self.stream.write(data)
  43. # empty queue
  44. self.queue.truncate(0)
  45. def writerows(self, rows):
  46. for row in rows:
  47. self.writerow(row)
  48. class UTF8Recoder(object):
  49. """
  50. Iterator that reads an encoded stream and reencodes the input to UTF-8
  51. """
  52. def __init__(self, f, encoding):
  53. self.reader = codecs.getreader(encoding)(f)
  54. def __iter__(self):
  55. return self
  56. def next(self):
  57. return self.reader.next().encode("utf-8")
  58. class CsvUnicodeReader(object):
  59. """
  60. A CSV reader which will iterate over lines in the CSV file "f",
  61. which is encoded in the given encoding.
  62. """
  63. def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwargs):
  64. f = UTF8Recoder(f, encoding)
  65. self.reader = csv.reader(f, dialect=dialect, **kwargs)
  66. def next(self):
  67. row = six.advance_iterator(self.reader)
  68. return [six.text_type(s).encode("utf-8") for s in row]
  69. def __iter__(self):
  70. return self