python迭代器中的最后一个元素

iom*_*tin 0 python

我想迭代文件的行,并为每个文件打印一些输出.,\n除最后一行外,所有打印的行最后都应该有一行.

我的第一种方法是使用查找hasNext()不存在的方法.我知道StopIteration引发了异常,但我不确定如何以Pythonic的方式使用它来实现我想要的.

Pav*_*sov 6

打印第一行,然后打印其他行",\n".

firstline = next(thefile)
print get_some_output(firstline)
for line in thefile:
    print ",\n" + get_some_output(line)
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

5356 次

最近记录:

13 年,2 月 前