Handle different encodings, use six
This commit is contained in:
parent
70558caa32
commit
cb8d65d8c0
2 changed files with 15 additions and 3 deletions
|
@ -1,5 +1,8 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import csv
|
||||
import os
|
||||
import six
|
||||
|
||||
|
||||
class Storage:
|
||||
|
@ -24,12 +27,12 @@ class Storage:
|
|||
if self.is_empty():
|
||||
write_header = True
|
||||
|
||||
with open(self._filename, 'a+') as csv_file:
|
||||
with open(self._filename, 'a+b') as csv_file:
|
||||
dict_writer = csv.DictWriter(csv_file, data[0].keys())
|
||||
if write_header:
|
||||
dict_writer.writeheader()
|
||||
|
||||
dict_writer.writerows(data)
|
||||
dict_writer.writerows(self._encode_data(data))
|
||||
|
||||
def read(self, limit=10, order='asc'):
|
||||
index = []
|
||||
|
@ -38,7 +41,7 @@ class Storage:
|
|||
return index
|
||||
|
||||
row_counter = 0
|
||||
with open(self._filename, 'r') as csv_file:
|
||||
with open(self._filename, 'rb') as csv_file:
|
||||
reader = csv.DictReader(csv_file)
|
||||
for row in reader:
|
||||
index.append(row)
|
||||
|
@ -57,3 +60,10 @@ class Storage:
|
|||
def clear(self):
|
||||
if os.path.exists(self._filename):
|
||||
os.remove(self._filename)
|
||||
|
||||
def _encode_data(self, data):
|
||||
for index, item in enumerate(data):
|
||||
for key, value in item.items():
|
||||
if isinstance(value, six.string_types):
|
||||
data[index][key] = value.encode('utf-8')
|
||||
return data
|
||||
|
|
|
@ -2,3 +2,5 @@ click==6.2
|
|||
pocket-api
|
||||
requests==2.9.1
|
||||
progress==1.2
|
||||
future==0.15.2
|
||||
six==1.10.0
|
||||
|
|
Loading…
Reference in a new issue