I figured out how to do it by extending the ExporterClass and adapting the
TSV exporter to handle csv with the field labels. Perhaps this should be an
option for the default ExporterCSV? Here it is for posterity:
class ExporterCSVlabel(ExportClass):
label = 'CSV (real labels)'
file_ext = "csv"
content_type = "text/csv"
def __init__(self, rows):
ExportClass.__init__(self, rows)
def export(self):
out = cStringIO.StringIO()
final = cStringIO.StringIO()
import csv
writer = csv.writer(out)
if self.rows:
import codecs
final.write(codecs.BOM_UTF16)
header = list()
for col in self.rows.colnames:
(t, f) = col.split('.')
field = self.rows.db[t][f]
field_label = field.label # Use the label name instead of
database name
colname = unicode(field_label).encode("utf8")
header.append(colname)
writer.writerow(header)
data = out.getvalue().decode("utf8")
data = data.encode("utf-16")
data = data[2:]
final.write(data)
out.truncate(0)
records = self.represented()
for row in records:
writer.writerow(
[str(col).decode('utf8').encode("utf-8") for col in row])
data = out.getvalue().decode("utf8")
data = data.encode("utf-16")
data = data[2:]
final.write(data)
out.truncate(0)
return str(final.getvalue())
On Friday, July 15, 2016 at 11:45:01 PM UTC-4, Jordan Myers wrote:
>
> Hey everyone,
>
> so the default csv exporters for sqlform.grid will use the headers as the
> 'database' names, but I would rather it use the field.label instead. For
> example, take this table:
>
> db.define_table('my_table', Field('ugly_name', label='Pretty Name'))
>
> A sqlform.grid on my_table will display the column with the header Pretty
> Name, but when exporting to csv, the header becomes my_table.ugly_name. Is
> there an easy way to export to csv with the columns "as seen" i.e. with the
> labeled names? I tried writing my own exporter, but it does not preserve
> the searching/sorting order. I suppose I could try to implement that
> myself, but that is a last resort.
>
> Thanks
>
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.