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

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

问题描述

当我使用 csv.reader 浏览文件时,如何返回到文件顶部.如果我用普通文件来做,我可以做类似file.seek(0)"的事情.csv 模块有类似的东西吗?

提前致谢;)

解决方案

可以直接查找文件.例如:

<预><代码>>>>f = open("csv.txt")>>>c = csv.reader(f)>>>对于 c 中的行:打印行['1', '2', '3']['4', '5', '6']>>>f.seek(0)>>>对于 c 中的行:再次打印行 #['1', '2', '3']['4', '5', '6']

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?

Thanks ahead of time ;)

解决方案

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天全站免登陆