【发布时间】:2012-10-09 21:40:08
【问题描述】:
我的代码的目的是遍历数组中的每个元素,将元素转换为字符串,然后从包含该字符串的另一个文件返回行。我的代码是:
for element in myarray:
elementstring=''.join(element)
for line in myfile:
if elementstring in line:
print line
如果代码运行,它只会对第一个元素起作用。谁能解释这是为什么?
【问题讨论】:
-
什么是
myfile?是文件对象还是字符串数组? -
myfile 是一个文件对象,用 myfile=open('location','r') 打开
标签: python file for-loop python-2.7 nested-loops