[dataio] ``parsefile()`` handles string or stream as ``filename``
authorSimon Chabot <simon.chabot@logilab.fr>
Thu, 29 Nov 2012 14:06:18 +0100
changeset 171 87249081b27d
parent 170 3679c8eab2be
child 172 0c1c26ddc444
[dataio] ``parsefile()`` handles string or stream as ``filename`` This patch aims to enable the user to give the path of the file she wants to open, or an already opened file. This is usefull because this function now handles files opened by urllib or whatever.
dataio.py
--- a/dataio.py	Tue Nov 27 20:17:49 2012 +0100
+++ b/dataio.py	Thu Nov 29 14:06:18 2012 +0100
@@ -101,10 +101,15 @@
         if field_size_limit:
             csv.field_size_limit(field_size_limit)
 
-        with open(filename, 'r') as csvfile:
-            reader = csv.reader(csvfile, delimiter=delimiter)
-            for row in reader:
-                yield [cell.strip() for cell in row]
+        if isinstance(filename, basestring):
+            csvfile = open(filename, 'r')
+        else:
+            csvfile = filename
+        reader = csv.reader(csvfile, delimiter=delimiter)
+        for row in reader:
+            yield [cell.strip() for cell in row]
+        csvfile.close()
+
 
 
     result = []