[Tutor] how-to generate specific lines of text from two python lists

Tahir Hafiz tahir.hafiz at gmail.com
Wed Jun 21 16:26:30 EDT 2017


Hi All,

My python skills are limited but I have managed to generate a couple
of lists using python and the psycopg2 library by querying a postgress
table and it's columns.
I would like to use the two generated lists from the python script to
create a file called upgrade_email_addresses.sql (and then later on
use the psql "postgress cli" with the -f flag against this .sql file)
which will be used to update the users database with the correct email
addresses.

Is there a way to generate a file from two lists? This is what I need to do ...

I have two lists such like:
listUsernames = ['adal', '', 'pascalb', 'ritchied', 'torvaldsl', ... ]
listEmailaddress = ['Ada_Lovelace at bigcorp.com',
'Blaise_Pascal at bigcorp.com', 'Dennis_Ritchie at bigcorp.com',
'Linus_Torvalds at bigcorp.com', ... ]


So in my python script I would like to generate a text file
(upgrade_email_addresses.sql) in the file system say in /tmp or /home,
that will contain the following lines by perhaps looping against the
lists in some way to create the lines in the external file:
UPDATE users set email='Ada_Lovelace at bigcorp.com' WHERE username='adal';
UPDATE users set email='Blaise_Pascal at bigcorp.com' WHERE username='pascalb';
UPDATE users set email='Dennis_Ritchie at bigcorp.com' WHERE username='ritchied';
UPDATE users set email='Linus_Torvalds at bigcorp.com' WHERE username='torvaldsl';
....
....

Any help would be much appreciated. I was thinking I could run the
UPDATE queries in the psycopg2 console function directly in my python
script but haven't been able to do that but now I'm thinking creating
a upgrade_email_addresses.sql file and then calling psql cli against
that would be easier.

Regards
Tahir


More information about the Tutor mailing list