Re: export .csv file from database

2013-07-19 Thread Peter of the Norse
Two things: 
First, Ms Excel sucks. From that attached screenshot it's obvious that it 
thought it was a space separated file. When actually using MS Office, I 
recommend that you add `quoting=csv.QUOTE_ALL` to all csv writers. 

Second, the HttpResponse object is file-like. You don't need to jump through 
all the hoops. And MS Office is going to ignore your encoding anyway, so 
there's no point in trying to fix it.

On Jun 17, 2013, at 9:44 AM, roopasingh...@gmail.com wrote:

> views.py
> 
> @login_required
> def export_csv(request):
> user = request.user
> # heading columns
> headerrow = ['Filename', 'Description','Notes other','Reporter Name']
> allrows = []
> today = datetime.datetime.today() # default to show today
> reports = Report.objects.filter(user=user)
> reports = reports.filter(created_date_time__year=today.year)
> for report in reports:
> row = []
> row.append(report.report_number);
> row.append(report.report_description);
> row.append(report.notes_other);
> row.append(report.reporter_name);
> allrows.append(row)
> # setup CSV export
> response = HttpResponse(mimetype='text/csv')
> filename = "%s-%s.csv" % ('Daily-reports', 
> datetime.datetime.now().strftime('%Y-%m-%d'))
> response['Content-Disposition'] = 'attachment; filename=%s' % filename
> writer = UnicodeWriter(response, encoding='utf-8')
> writer.writerow(headerrow)
> for writerow in allrows:
> writer.writerow(writerow)
> return response
> 
> csv_unicode.py
> 
> import csv, codecs, cStringIO
> class UnicodeWriter:
>def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
> # Redirect output to a queue
> self.queue = cStringIO.StringIO()
> self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
> self.stream = f
> self.encoder = codecs.getincrementalencoder(encoding)()
> 
> def writerow(self, row):
> self.writer.writerow([unicode(s).encode("utf-8") for s in row])
> # Fetch UTF-8 output from the queue ...
> data = self.queue.getvalue()
> data = data.decode("utf-8")
> # ... and reencode it into the target encoding
> data = self.encoder.encode(data)
> # write to the target stream
> self.stream.write(data)
> # empty queue
> self.queue.truncate(0)
> 
> def writerows(self, rows):
> for row in rows:
> self.writerow(row)
> 
> The above code is exporting the database data into .csv file.
> 
> Problem i am facing is,the exported data like Filename,Description,Notes 
> Others,Reporter Name are exported in separate cell.Will show the output
>  i am getting and required output.
> 
> You can see my existing and required format. 
> 
> I had attached two format,file name existing_csv is the output file i ma 
> getting,required_csv is the .csv file i am expecting.
> 
> Need some ones help.

Peter of the Norse
rahmc...@radio1190.org



-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.




Re: export .csv file from database

2013-06-19 Thread Radomir Wojcik

>
> this might come of use, it exports any query set to csv. I use it a lot:
>
>
def queryset_export_csv(qs):
import csv
#from django.db.models.loading import get_model

response = HttpResponse(mimetype='text/csv')
# force download.
response['Content-Disposition'] = 
'attachment;filename="service_export.csv"'

# the csv writer
writer = csv.writer(response)

qs_model = qs.model

headers = []
for field in qs_model._meta.fields:
headers.append(field.name)
writer.writerow(headers)

for obj in qs:
row = []
for field in headers:
val = getattr(obj, field)
if callable(val):
val = val()
if type(val) == unicode:
val = val.encode("utf-8")
row.append(val)
writer.writerow(row)

return response 

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.




Re: export .csv file from database

2013-06-19 Thread Vernon D. Cole
 I am guessing that your csv writer is outputting something that your 
spreadsheet is reading incorrectly.  Try opening the csv with a text editer 
(a good one, like Notepad++) so that you can really see what is going on. 
Embedded commas or quotes inside a field are invitations for disaster, and 
you have both.  You may need to change the settings on the csv writer -- it 
has lots of them, and different spreadsheet programs interpret different 
csv files differently.  The default settings are usually good for Excel, 
but you don't mention which spreadsheet you are trying to do the reading 
with, nor what settings you are using at import time.  You must find a 
combination that works for your situation (and it will not be easy.)  It 
may actually be easier to re-design the target system than to meet its 
existing requirements.
  Another option would be to use a tool like xlwt and send out a real 
spreadsheet rather than a .csv.  That way, all interpretation is under your 
control.  


On Monday, June 17, 2013 4:44:39 PM UTC+1, roopas...@gmail.com wrote:
>
> views.py
>
> @login_requireddef export_csv(request):
> user = request.user
> # heading columns
> headerrow = ['Filename', 'Description','Notes other','Reporter Name']
> allrows = []
> today = datetime.datetime.today() # default to show today
> reports = Report.objects.filter(user=user)
> reports = reports.filter(created_date_time__year=today.year)
> for report in reports:
> row = []
> row.append(report.report_number);
> row.append(report.report_description);
> row.append(report.notes_other);
> row.append(report.reporter_name);
> allrows.append(row)
> # setup CSV export
> response = HttpResponse(mimetype='text/csv')
> filename = "%s-%s.csv" % ('Daily-reports', 
> datetime.datetime.now().strftime('%Y-%m-%d'))
> response['Content-Disposition'] = 'attachment; filename=%s' % filename
> writer = UnicodeWriter(response, encoding='utf-8')
> writer.writerow(headerrow)
> for writerow in allrows:
> writer.writerow(writerow)
> return response
>
> csv_unicode.py
>
> import csv, codecs, cStringIOclass UnicodeWriter:
>def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
> # Redirect output to a queue
> self.queue = cStringIO.StringIO()
> self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
> self.stream = f
> self.encoder = codecs.getincrementalencoder(encoding)()
>
> def writerow(self, row):
> self.writer.writerow([unicode(s).encode("utf-8") for s in row])
> # Fetch UTF-8 output from the queue ...
> data = self.queue.getvalue()
> data = data.decode("utf-8")
> # ... and reencode it into the target encoding
> data = self.encoder.encode(data)
> # write to the target stream
> self.stream.write(data)
> # empty queue
> self.queue.truncate(0)
>
> def writerows(self, rows):
> for row in rows:
> self.writerow(row)
>
> The above code is exporting the database data into .csv file.
>
> Problem i am facing is,the exported data like Filename,Description,Notes 
> Others,Reporter Name are exported in separate cell.Will show the output
>  i am getting and required output.
>
> You can see my existing and required format. 
>
> I had attached two format,file name existing_csv is the output file i ma 
> getting,required_csv is the .csv file i am expecting.
>
> Need some ones help.
>
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.




Re: export .csv file from database

2013-06-19 Thread Derek
>From just a quick glance, I have probably missed complex here - but one 
option to try is changing the delimiter from a comma to another character 
(e.g. a semi-colon) - see:
http://docs.python.org/2/library/csv.html#dialects-and-formatting-parameters
(But its also possible that in all the encoding/decoding that is going on, 
that something else is going wrong!)

On Monday, 17 June 2013 17:44:39 UTC+2, roopas...@gmail.com wrote:
>
> views.py
>
> @login_requireddef export_csv(request):
> user = request.user
> # heading columns
> headerrow = ['Filename', 'Description','Notes other','Reporter Name']
> allrows = []
> today = datetime.datetime.today() # default to show today
> reports = Report.objects.filter(user=user)
> reports = reports.filter(created_date_time__year=today.year)
> for report in reports:
> row = []
> row.append(report.report_number);
> row.append(report.report_description);
> row.append(report.notes_other);
> row.append(report.reporter_name);
> allrows.append(row)
> # setup CSV export
> response = HttpResponse(mimetype='text/csv')
> filename = "%s-%s.csv" % ('Daily-reports', 
> datetime.datetime.now().strftime('%Y-%m-%d'))
> response['Content-Disposition'] = 'attachment; filename=%s' % filename
> writer = UnicodeWriter(response, encoding='utf-8')
> writer.writerow(headerrow)
> for writerow in allrows:
> writer.writerow(writerow)
> return response
>
> csv_unicode.py
>
> import csv, codecs, cStringIOclass UnicodeWriter:
>def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
> # Redirect output to a queue
> self.queue = cStringIO.StringIO()
> self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
> self.stream = f
> self.encoder = codecs.getincrementalencoder(encoding)()
>
> def writerow(self, row):
> self.writer.writerow([unicode(s).encode("utf-8") for s in row])
> # Fetch UTF-8 output from the queue ...
> data = self.queue.getvalue()
> data = data.decode("utf-8")
> # ... and reencode it into the target encoding
> data = self.encoder.encode(data)
> # write to the target stream
> self.stream.write(data)
> # empty queue
> self.queue.truncate(0)
>
> def writerows(self, rows):
> for row in rows:
> self.writerow(row)
>
> The above code is exporting the database data into .csv file.
>
> Problem i am facing is,the exported data like Filename,Description,Notes 
> Others,Reporter Name are exported in separate cell.Will show the output
>  i am getting and required output.
>
> You can see my existing and required format. 
>
> I had attached two format,file name existing_csv is the output file i ma 
> getting,required_csv is the .csv file i am expecting.
>
> Need some ones help.
>
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.




export .csv file from database

2013-06-17 Thread roopasingh250
views.py

@login_requireddef export_csv(request):
user = request.user
# heading columns
headerrow = ['Filename', 'Description','Notes other','Reporter Name']
allrows = []
today = datetime.datetime.today() # default to show today
reports = Report.objects.filter(user=user)
reports = reports.filter(created_date_time__year=today.year)
for report in reports:
row = []
row.append(report.report_number);
row.append(report.report_description);
row.append(report.notes_other);
row.append(report.reporter_name);
allrows.append(row)
# setup CSV export
response = HttpResponse(mimetype='text/csv')
filename = "%s-%s.csv" % ('Daily-reports', 
datetime.datetime.now().strftime('%Y-%m-%d'))
response['Content-Disposition'] = 'attachment; filename=%s' % filename
writer = UnicodeWriter(response, encoding='utf-8')
writer.writerow(headerrow)
for writerow in allrows:
writer.writerow(writerow)
return response

csv_unicode.py

import csv, codecs, cStringIOclass UnicodeWriter:
   def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
# Redirect output to a queue
self.queue = cStringIO.StringIO()
self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
self.stream = f
self.encoder = codecs.getincrementalencoder(encoding)()

def writerow(self, row):
self.writer.writerow([unicode(s).encode("utf-8") for s in row])
# Fetch UTF-8 output from the queue ...
data = self.queue.getvalue()
data = data.decode("utf-8")
# ... and reencode it into the target encoding
data = self.encoder.encode(data)
# write to the target stream
self.stream.write(data)
# empty queue
self.queue.truncate(0)

def writerows(self, rows):
for row in rows:
self.writerow(row)

The above code is exporting the database data into .csv file.

Problem i am facing is,the exported data like Filename,Description,Notes 
Others,Reporter Name are exported in separate cell.Will show the output
 i am getting and required output.

You can see my existing and required format. 

I had attached two format,file name existing_csv is the output file i ma 
getting,required_csv is the .csv file i am expecting.

Need some ones help.



-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.


<><>