Python检测EOF [英] Python detecting EOF
本文介绍了Python检测EOF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在读取csv文件时编写一条if语句:
I'm trying to write an if statement that goes as such, while reading a csv file:
if row = [] or EOF:
do stuff
我已经在线搜索过,找不到任何方法.帮助吗?
I've searched online and couldn't find any way of doing this. Help?
推荐答案
with open(fname, 'rb') as f:
for line in f:
# line = line.strip(' \r\n') # to remove spaces and new line chars if needed
if not line:
do stuff
do stuff
以上已足够.
要检查您是否位于文件末尾,也可以执行以下操作:
To check if you are in the end of file you can also do:
import os
with open(fname, 'rb') as f:
is_end = f.tell() == os.fstat(f.fileno()).st_size
但是我认为你不需要.
这篇关于Python检测EOF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文