[英]Why is Python concatenating strings in a list rather than iterating over them?
In a Python CGI script, I have lists of strings which are keys to a hash: 在Python CGI脚本中,我有一些字符串列表,这些字符串是哈希键:
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: Apache日志忠实记录:
[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. 令我惊讶的是,它似乎在单个字符串上进行了一次迭代(列表的多余连接),而不是在repr
似乎识别为字符串列表的基础上进行了迭代,这正是我的意图。
How can I get the loop to iterate over 'Alfresco', 'Bible', etc. up through 'CJSHayward'? 如何通过“ CJSHayward”向上循环遍历“ Alfresco”,“ Bible”等?
As commenters have indicated, you must append a newline character ( \\n
) to website
in order for each website to appear on it's own line in the Apache log. 正如评论者所指出的那样,您必须在website
添加换行符( \\n
),以便每个网站都可以在Apache日志中显示在其自己的行上。
for website in MAIN_WEBSITES + APPENDIX_WEBSITES:
sys.stderr.write(website + '\n')
That is will resolve your issue. 那将解决您的问题。 However, it may interest you to know why this resolves the issue . 但是, 您可能想知道为什么这可以解决问题 。
It's not crazy to expect that each call to sys.stderr.write
would result its own line in the log. 期望每次对sys.stderr.write
调用都会在日志中产生自己的行并不奇怪。 The reason this is not the case is because sys.stderr
is a stream. 并非如此的原因是因为sys.stderr
是流。 A stream is an abstraction for data being sent somewhere. 流是将数据发送到某处的抽象。 In this case, sys.stderr.write
writes to the standard error stream, and your Apache log reads from the standard error stream. 在这种情况下, sys.stderr.write
将写入标准错误流,而您的Apache日志将从标准错误流中读取 。 The Apache log does not know how many times you have called sys.stderr.write
. Apache日志不知道您调用sys.stderr.write
。 It only knows what it can see in the stream. 它只知道在流中可以看到的内容。 For example, it is impossible for the Apache log to know the difference between 例如,Apache日志不可能知道两者之间的区别
sys.stderr('HelloWorld')
and 和
sys.stderr('Hello')
sys.stderr('World')
In both cases, Apache log (and other applications that read from standard error) just see the stream of characters 'HelloWorld'
. 在这两种情况下,Apache日志(以及从标准错误中读取的其他应用程序)仅能看到字符流'HelloWorld'
。 For this reason, Apache log must use the newline character ( \\n
) to divide the standard error stream into separate lines. 因此,Apache日志必须使用换行符( \\n
)将标准错误流划分为单独的行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.