on master help sign in
[fix] resurrect csv output in py2
3d6c679Adam Tauber
searx/utils.py+13−9
@@ -29,6 +29,9 @@ except:
29if sys.version_info[0] == 3:
29if sys.version_info[0] == 3:
30 unichr = chr
30 unichr = chr
31 unicode = str
31 unicode = str
32+ IS_PY2 = False
33+else:
34+ IS_PY2 = True
32
35
33logger = logger.getChild('utils')
36logger = logger.getChild('utils')
34
37
@@ -159,19 +162,20 @@ class UnicodeWriter:
159 self.encoder = getincrementalencoder(encoding)()
162 self.encoder = getincrementalencoder(encoding)()
160
163
161 def writerow(self, row):
164 def writerow(self, row):
162− unicode_row = []
165+ if IS_PY2:
163− for col in row:
166+ row = [s.encode("utf-8") if hasattr(s, 'encode') else s for s in row]
164− if type(col) == str or type(col) == unicode:
165− unicode_row.append(col.encode('utf-8').strip())
166− else:
167− unicode_row.append(col)
168− self.writer.writerow([x.decode('utf-8') if hasattr(x, 'decode') else x for x in unicode_row])
167+ self.writer.writerow(row)
169 # Fetch UTF-8 output from the queue ...
168 # Fetch UTF-8 output from the queue ...
170− data = self.queue.getvalue().strip('\x00')
169+ data = self.queue.getvalue()
170+ if IS_PY2:
171+ data = data.decode("utf-8")
171 # ... and reencode it into the target encoding
172 # ... and reencode it into the target encoding
172 data = self.encoder.encode(data)
173 data = self.encoder.encode(data)
173 # write to the target stream
174 # write to the target stream
175+ if IS_PY2:
176+ self.stream.write(data)
177+ else:
174− self.stream.write(data.decode('utf-8'))
178+ self.stream.write(data.decode("utf-8"))
175 # empty queue
179 # empty queue
176 self.queue.truncate(0)
180 self.queue.truncate(0)
177
181