Python csv.reader:如何返回到文件的顶部? [英] Python csv.reader: How do I return to the top of the file?

查看:213
本文介绍了Python csv.reader:如何返回到文件的顶部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用csv.reader移动文件时,如何返回到文件的顶部。如果我做一个正常的文件,我可以做一些像file.seek(0)。

When I'm moving through a file with a csv.reader, how do I return to the top of the file. If I were doing it with a normal file I could just do something like "file.seek(0)". Is there anything like that for the csv module?

提前感谢;)

推荐答案

您可以直接寻找文件。例如:

You can seek the file directly. For example:

>>> f = open("csv.txt")
>>> c = csv.reader(f)
>>> for row in c: print row
['1', '2', '3']
['4', '5', '6']
>>> f.seek(0)
>>> for row in c: print row   # again
['1', '2', '3']
['4', '5', '6']

这篇关于Python csv.reader:如何返回到文件的顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆