[Csv] skipfinalspace

Tom Brown tom.brown.code at gmail.com
Sun Oct 19 01:15:01 CEST 2008


Hello python csv gurus!
I use the csv module pretty heavily in
http://code.google.com/p/googletransitdatafeed/source/browse/trunk/python/transitfeed.pyand
someone recently complained that it doesn't handle white space before
and after fields. I can fix this skipinitialspace and a little
post-processing to remove trailing whitespace but thought it would be nice
to add skipifinalspace to the csv module.
We have 476 feeds generated by different tools (plugins to various
proprietary software, Python's csv, by hand, Excel, ...). Of these 9 have a
space after fields in the header and 22 have spaces before fields in the
header.

I downloaded the 2.6 source tar ball, but is it too late for new features to
get into versions <3?

How would you feel about adding the following tests to Lib/test/test_csv.py
and getting them to pass?

Also http://www.python.org/doc/2.5.2/lib/csv-fmt-params.html says
"*skipinitialspace *When True, whitespace immediately following the
delimiter is ignored."
but my tests show whitespace at the start of any field is ignored, including
the first field.

Thanks,
Tom

class TestDialectOption(TestCsvBase):
    @staticmethod
    def makeDialect(dct):
      name = "dialect-%d" % (hash(tuple(dct.items())))
      return type(name, (csv.excel, object), dct)()

    def test_no_skip(self):
      self.dialect = self.makeDialect({})
      self.readerAssertEqual(' foo,bar', [[' foo', 'bar']])
      self.readerAssertEqual('foo, bar', [['foo', ' bar']])
      self.readerAssertEqual(' foo, bar', [[' foo', ' bar']])
      self.readerAssertEqual(' foo , bar', [[' foo ', ' bar']])
      self.readerAssertEqual(' foo , bar ', [[' foo ', ' bar ']])

    def test_skip_initial(self):
      self.dialect = self.makeDialect({"skipinitialspace": True})
      self.readerAssertEqual(' foo,bar', [['foo', 'bar']])
      self.readerAssertEqual('foo, bar', [['foo', 'bar']])
      self.readerAssertEqual(' foo, bar', [['foo', 'bar']])
      self.readerAssertEqual(' foo , bar', [['foo ', 'bar']])
      self.readerAssertEqual(' foo , bar ', [['foo ', 'bar ']])

    def test_skip_final(self):
      self.dialect = self.makeDialect({"skipfinalspace": True})
      self.readerAssertEqual(' foo,bar', [[' foo', 'bar']])
      self.readerAssertEqual('foo, bar', [['foo', ' bar']])
      self.readerAssertEqual(' foo, bar', [[' foo', ' bar']])
      self.readerAssertEqual(' foo , bar', [[' foo', ' bar']])
      self.readerAssertEqual(' foo , bar ', [[' foo', ' bar']])

    def test_skip_both(self):
      self.dialect = self.makeDialect({"skipinitialspace": True,
                                       "skipfinalspace": True})
      self.readerAssertEqual(' foo,bar', [['foo', 'bar']])
      self.readerAssertEqual('foo, bar', [['foo', 'bar']])
      self.readerAssertEqual(' foo, bar', [['foo', 'bar']])
      self.readerAssertEqual(' foo , bar', [['foo', 'bar']])
      self.readerAssertEqual(' foo , bar ', [['foo', 'bar']])
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/csv/attachments/20081018/cb4dcf85/attachment.htm>


More information about the Csv mailing list