【发布时间】:2016-06-30 00:08:18
【问题描述】:
我的代码
def yieldlines(thefile, whatlines):
return (x for i, x in enumerate(thefile) if i in whatlines)
file1=open('/home/milenko/EDIs/site1/newst2.txt','r')
whatlines1 = [line.strip() for line in open('m1.dat', 'r')]
x1=yieldlines(file1, whatlines1)
print x1
我明白了
<generator object <genexpr> at 0x7fa3cd3d59b0>
列表应该放在哪里,还是需要重写代码?
我希望我的程序写入文件并读取内容,因此对于在 m1.dat 中写入的特定行。我找到了解决方案 Reading specific lines only (Python)
【问题讨论】:
-
请准确解释您要做什么。因为你刚刚得到的是一个generator,没有什么真的错的。如果你迭代它,你会得到你的输出。但是您需要注意一些差异。请务必阅读我提供的链接。