Преглед на файлове

[fix] resurrect csv output in py2

Adam Tauber преди 7 години
родител
ревизия
3d6c67951a
променени са 1 файла, в които са добавени 13 реда и са изтрити 9 реда
  1. 13
    9
      searx/utils.py

+ 13
- 9
searx/utils.py Целия файл

@@ -29,6 +29,9 @@ except:
29 29
 if sys.version_info[0] == 3:
30 30
     unichr = chr
31 31
     unicode = str
32
+    IS_PY2 = False
33
+else:
34
+    IS_PY2 = True
32 35
 
33 36
 logger = logger.getChild('utils')
34 37
 
@@ -159,19 +162,20 @@ class UnicodeWriter:
159 162
         self.encoder = getincrementalencoder(encoding)()
160 163
 
161 164
     def writerow(self, row):
162
-        unicode_row = []
163
-        for col 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])
165
+        if IS_PY2:
166
+            row = [s.encode("utf-8") if hasattr(s, 'encode') else s for s in row]
167
+        self.writer.writerow(row)
169 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 172
         # ... and reencode it into the target encoding
172 173
         data = self.encoder.encode(data)
173 174
         # write to the target stream
174
-        self.stream.write(data.decode('utf-8'))
175
+        if IS_PY2:
176
+            self.stream.write(data)
177
+        else:
178
+            self.stream.write(data.decode("utf-8"))
175 179
         # empty queue
176 180
         self.queue.truncate(0)
177 181