我正在使用从数据仓库(Cognos)获得的CSV文件进行数据分析。CSV文件的最后一行总结了上面的所有行,但是我不需要这一行来进行分析,所以我想跳过最后一行。
我正在考虑添加“if”语句来检查“for”循环中的列名,如下所示。
import CSV
with open('COGNOS.csv', "rb") as f, open('New_COGNOS.csv', "wb") as w:
#Open 2 CSV files. One to read and the other to save.
CSV_raw = csv.reader(f)
CSV_new = csv.writer(w)
for row in CSV_raw:
item_num = row[3].split(" ")[0]
row.append(item_num)
if row[0] == "All Materials (By Collection)": break
CSV_new.writerow(row)
然而,这看起来像是在浪费大量资源。在CSV文件中迭代时,有没有pythonian方法跳过最后一行?
目前没有回答
相关问题 更多 >
编程相关推荐