I am trying to create a csv file using python that is truly Excel-compatible (I am using Excel 2007, if that makes any difference).
Here is the sort of thing I'm trying to do
import csv
data = [ ['9-1', '9-2', '9-3'] ]
fd = open('test.csv', 'wb')
try:
writer = csv.writer(fd, dialect='excel', quotechar='"', quoting=csv.QUOTE_ALL)
writer.writerows(data)
finally:
fd.close()
This creates a csv file that contains:
"9-1","9-2","9-3"
When I load it in Excel, I get the columns:
09-Jan 09-Feb 09-Mar
[Grrr, thanks Excel. Seriously, what is the point of quotes?]
Now, I have read elsewhere that in order to get Excel to treat strings as literal they must be prefixed by an equals sign ="Like This"
. So what I really want to produce is:
="9-1",="9-2",="9-3"
Is there any way to do this with the csv
module in python? I'm guessing it will involve creating a custom dialect, but I can't see any variable that would give me the desired result.