Best way to inplace alter a list going into postgres
Sayth Renshaw
flebber.crue at gmail.com
Tue May 31 02:19:50 EDT 2016
On Tuesday, 31 May 2016 15:58:40 UTC+10, Steven D'Aprano wrote:
> On Tuesday 31 May 2016 14:27, Sayth Renshaw wrote:
>
> >
> > Hi
> >
> > What is the best way to inplace alter a list going into a postgres database
>
> Is it relevant where it is going?
>
> What you do with the list after you alter it is irrelevant -- perhaps you will
> insert it into an Oracle database, or a place it in a dict, or print it. The
> only reason it might be relevant is if Postgres can do the work for you, so you
> don't need to split it in Python. And that's probably a question for a Postgres
> group, not a Python group.
>
According to this http://stackoverflow.com/a/29420035/461887
http://stackoverflow.com/a/8767450/461887
Postgres seems like it could perform it, I need to research further.
>
> Perhaps a better way is to build a new list:
>
> L = ['x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', '11-3-1-4 $72390.00',
> '2-0-0-2 $8970.00', '3-2-0-0 $30085.00', '3-1-0-0 $15450.00',
> 'x', 'x', 'x']
> new = []
> for i, item in enumerate(L):
> if i in (9, 10, 11, 12):
> values = item.replace('-', ' ').split()
> new.extend(values)
> else:
> new.append(values)
>
>
>
> You can then replace L with new *in-place* with a slice:
>
> L[:] = new
>
> or just use new instead.
>
>
>
> --
> Steve
So with that cool enumerate example I need to decide whether its safer or more efficient to put the data directly into postgres and then modify it there with postgres methods or use python to modify prior to entry.
Need more research.
Thanks
Sayth
More information about the Python-list
mailing list