In a Python CGI script, I have lists of strings which are keys to a hash:
APPENDIX_WEBSITES = ['CJSHayward']
MAIN_WEBSITES = ['Alfresco',
'Bible',
'Fathers',
'MyCollab',
'Koha',
'MediaWiki',
'Moodle',
'RequestTracker',
'SuiteCRM',
'TikiWiki',
'Wordpress']
# The variable "data" is populated with a hash containing all above entries as keys.
sys.stderr.write(repr(MAIN_WEBSITES) + '\n')
sys.stderr.write(repr(APPENDIX_WEBSITES) + '\n')
sys.stderr.write(repr(MAIN_WEBSITES + APPENDIX_WEBSITES) + '\n')
for website in MAIN_WEBSITES + APPENDIX_WEBSITES:
sys.stderr.write(website)
The Apache log faithfully records:
[Tue Aug 08 16:25:34.266769 2017] [cgi:error] [pid 16429] [client 127.0.0.1:40600] AH01215: ['Alfresco', 'Bible', 'Fathers', 'MyCollab', 'Koha', 'MediaWiki', 'Moodle', 'RequestTracker', 'SuiteCRM', 'TikiWiki', 'Wordpress']: /usr/local/websites/home/www/configure/index.cgi, referer: http://localhost/ [Tue Aug 08 16:25:34.267050 2017] [cgi:error] [pid 16429] [client 127.0.0.1:40600] AH01215: ['CJSHayward']: /usr/local/websites/home/www/configure/index.cgi, referer: http://localhost/ [Tue Aug 08 16:25:34.267268 2017] [cgi:error] [pid 16429] [client 127.0.0.1:40600] AH01215: ['Alfresco', 'Bible', 'Fathers', 'MyCollab', 'Koha', 'MediaWiki', 'Moodle', 'RequestTracker', 'SuiteCRM', 'TikiWiki', 'Wordpress', 'CJSHayward']: /usr/local/websites/home/www/configure/index.cgi, referer: http://localhost/ [Tue Aug 08 16:25:34.267490 2017] [cgi:error] [pid 16429] [client 127.0.0.1:40600] AH01215: AlfrescoAlfrescoBibleBibleFathersFathersMyCollabMyCollabKohaKohaMediaWikiMediaWikiMoodleMoodleRequestTrackerRequestTrackerSuiteCRMSuiteCRMTikiWikiTikiWiki: /usr/local/websites/home/www/configure/index.cgi, referer: http://localhost/
I am surprised that it seems to be iterating once over a single string (a redundant concatenation of the list), instead of iterating over what repr
seems to recognize as a list of strings, which is what I intended.
How can I get the loop to iterate over 'Alfresco', 'Bible', etc. up through 'CJSHayward'?
sys.stderr.write(repr(MAIN_WEBSITES) + '\n')
is just writing the list as a string with a newline appended. – GH05Tfor website in MAIN_WEBSITES + APPENDIX_WEBSITES: sys.stderr.write(website)
iterates the concatenated lists, butsys.stderr.write
does not append a newline by default. So, it appears that you are getting concatenation of strings, but you're not. If you want a newline after each call towrite
, you must be explicit:sys.stderr.write('{}\n'.format(website))
– GH05T+ '\n'
inside your loop. Voting to close as typo. – Mad Physicistprint(<whatever>, file=sys.stderr)
for greater ease – juanpa.arrivillaga