在读取csv文件时,我试图编写这样的if语句:
if row = [] or EOF:
do stuff我在网上搜索过,找不到任何方法。帮助?
发布于 2014-08-26 16:02:14
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以上各点已足够。
若要检查是否处于文件末尾,还可以:
import os
with open(fname, 'rb') as f:
is_end = f.tell() == os.fstat(f.fileno()).st_size但我觉得你不需要。
发布于 2014-08-26 16:12:00
不确定我是否完全理解您,但是忽略空行,我将使用if line.strip()。
with open("in.txt") as f:
for line in f:
if line.strip():
# append
else:
# do what you need
# do last requirementhttps://stackoverflow.com/questions/25510222
复制相似问题