[Numpy-svn] r6330 - in trunk/numpy/lib: . tests

numpy-svn@scip... numpy-svn@scip...
Wed Jan 21 23:37:38 CST 2009


Author: pierregm
Date: 2009-01-21 23:37:36 -0600 (Wed, 21 Jan 2009)
New Revision: 6330

Modified:
   trunk/numpy/lib/io.py
   trunk/numpy/lib/tests/test_io.py
Log:
* genfromtxt : if names is True, accept a line starting with a comment character as header.

Modified: trunk/numpy/lib/io.py
===================================================================
--- trunk/numpy/lib/io.py	2009-01-19 21:22:52 UTC (rev 6329)
+++ trunk/numpy/lib/io.py	2009-01-22 05:37:36 UTC (rev 6330)
@@ -748,7 +748,14 @@
         first_line = fhd.readline()
         if first_line == '':
             raise IOError('End-of-file reached before encountering data.')
-        first_values = split_line(first_line)
+        if names is True:
+            first_values = first_line.strip().split(delimiter)
+        else:
+            first_values = split_line(first_line)
+    if names is True:
+        fval = first_values[0].strip()
+        if fval in comments:
+            del first_values[0]
 
     # Check the columns to use
     if usecols is not None:

Modified: trunk/numpy/lib/tests/test_io.py
===================================================================
--- trunk/numpy/lib/tests/test_io.py	2009-01-19 21:22:52 UTC (rev 6329)
+++ trunk/numpy/lib/tests/test_io.py	2009-01-22 05:37:36 UTC (rev 6330)
@@ -483,6 +483,30 @@
         assert_equal(test, control)
 
 
+    def test_commented_header(self):
+        "Check that names can be retrieved even if the line is commented out."
+        data = StringIO.StringIO("""
+#gender age weight
+M   21  72.100000
+F   35  58.330000
+M   33  21.99
+        """)
+        # The # is part of the first name and should be deleted automatically.
+        test = np.genfromtxt(data, names=True, dtype=None)
+        ctrl = np.array([('M', 21, 72.1), ('F', 35, 58.33), ('M', 33, 21.99)],
+                  dtype=[('gender','|S1'), ('age', int), ('weight', float)])
+        assert_equal(test, ctrl)
+        # Ditto, but we should get rid of the first element
+        data = StringIO.StringIO("""
+# gender age weight
+M   21  72.100000
+F   35  58.330000
+M   33  21.99
+        """)
+        test = np.genfromtxt(data, names=True, dtype=None)
+        assert_equal(test, ctrl)
+
+
     def test_autonames_and_usecols(self):
         "Tests names and usecols"
         data = StringIO.StringIO('A B C D\n aaaa 121 45 9.1')
@@ -707,9 +731,9 @@
                            dtype=[('a', np.int), ('b', np.int)])
         self.failUnless(isinstance(test, np.recarray))
         assert_equal(test, control)
-        
 
 
 
+
 if __name__ == "__main__":
     run_module_suite()



More information about the Numpy-svn mailing list