[Tutor] manipulting CSV files

Kent Johnson kent37 at tds.net
Fri Jan 8 14:11:45 CET 2010


On Thu, Jan 7, 2010 at 1:26 PM, Lowell Tackett <lowelltackett at yahoo.com> wrote:
> I found the Python documentation (on line} and came across--'csv.Dialect.skipinitialspace' which I believe is the answer to my dilemma.  However, my effort to implement that detail, based on interpreting the skimpy examples in the documentation:
>
> >>> coord = csv.reader(open('true_coord'),csv.Dialect.skipinitialspace = True)
>
> got me roundly shot down with:
>
>   File "<stdin>", line 1
> SyntaxError: keyword can't be an expression

Try
coord = csv.reader(open('true_coord', 'b'), skipinitialspace = True)

Or you could create a Dialect and pass it as a second argument to open(). Try
dialect = csv.excel() # start with the default dialect
dialect.skipinitialspace = True
coord = csv.reader(open('true_coord', 'b'), dialect)

Note you should open the file in binary mode.

Kent


More information about the Tutor mailing list