There is a built in method for this:
db(db.mytable).select(db.mytable.myfield).export_to_csv(open('/path/to/file'
, 'wb'))
If you don't want it to write the column name at the top of the file, pass
write_colnames=False to .export_to_csv().
Anthony
On Sunday, January 1, 2017 at 11:25:32 AM UTC-5, yarecki wr wrote:
>
>
>
> On Thursday, December 22, 2016 at 12:47:43 AM UTC+1, Dave S wrote:
>>
>> On Wednesday, December 21, 2016 at 7:05:57 AM UTC-8, yarecki wr wrote:
>>>
>>> I have a table defined where I would need to export one column to SMB
>>> share as a txt file automatically when a new row gets added? How can this
>>> be achieved? Hope it's clear from the question I'm beggining with web2py
>>> and python for that matter :)
>>>
>>
>>
>> A starting point for an answer would be: You query should ask the
>> database for just that column. You then convert the Rows object that's
>> returned (roughly speaking, an array of dicts of values that were found in
>> that column) into text. The simplest-to-describe method for that is to
>> loop through the Rows object, and write the value of the column (because
>> the value is in a dict, you have to use the fieldname to get the value).
>>
>> Something like:
>> openmyfile()
>> rows=db(mytlable.id > 0).select("bestfieldever");
>> for row in rows:
>> write2myfile(row["bestfieldever"]
>> closemyfile()
>>
>>
>> You can turn the Rows object into a list object, and perhaps writing the
>> list object works, so you don't have to do the for loop yourself.
>> <URL:
>> http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#as_dict-and-as_list
>> >
>>
>> You may also be able to use the CSV export tool:
>> <URL:
>> http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#CSV--one-Table-at-a-time-
>> >
>>
>> Good luck!
>>
>> /dps
>>
>>
> So this code should be in my default.py right? Does this need to be a part
> of some function?
> So I did
> open('/tmp/items.txt','w')
> rows=db(mytable.id>0).select("Property1")
> for row in rows:
> write(row["Property1])
> close('/tmp/items.txt)
>
> which gets me to <type 'exception.NameError'>(name 'write' is not defined)
>
> saying that I tried a slight modification I found on the web
> import os
> myfile=os.path.join(request.folder'static','/tmp/items.txt')
> file=open(myfile,'w')
> rows=db(mytable.id>0).select("Property1")
> for row in rows:
> file.write(row["Property1])
> file.close()
>
>
> This seem to have created the items.txt file. Unfortunately it must have
> created it under the web2py user account and I can't view the contents not
> having access to the file on the system. Looking for a way to view it in
> web2py now.
>
--
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.