简体   繁体   English

为什么Python在列表中串联字符串而不是遍历它们?

[英]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.

相关问题 遍历列表并在Python中串联字母顺序 - iterating over list and concatenating alphabetical sequence in Python 为什么遍历列表比遍历python中的迭代器要慢得多? - Why would iterating over a list be much slower than iterating over an iterator in python? 遍历python中的字符串 - Iterating over strings in python Python:为什么迭代一个列表比迭代其长度上的xrange()生成器定义要快? - Python: why is it faster to iterate over a list than iterating over an xrange() generator define on its length? 迭代两个不同长度的数据帧列表并将它们作为数据帧连接在一个循环中以执行 function - Iterating over two list of dataframes of different length and concatenating as dataframes them within a loop to perform a function 连接两个字符串时,为什么Python比C更快? - Why is Python faster than C when concatenating two strings? 为什么Python中的“执行”操作比迭代列表要快? - Why is “in” operation in Python faster than iterating a list? Python:遍历字符串列表并将它们与类对象相关联 - Python: Iterating Through a List of Strings and Associating Them With Class Objects 将列表名称连接成字符串 python - Concatenating list names into strings python 在 Python 中的字典(键和值)上迭代字符串列表 - Iterating a list of strings over a dictionary (keys and values) in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM