[英]Python skip operation on first line of file
I have a csv file which looks like following: 我有一个csv文件,如下所示:
CCC;reserved;reserved;pIndex;wedgeWT;NA;NA;NA;NA;NA;xOffset;yOffset;zOffset
0.10089,0,0,1,0,0,0,0,0,0,-1.8,-0.7,1999998
0.1124,0,0,3,0,0,0,0,0,0,-1.2,1.8,-3.9
I am using the fileinput
method to do some operation in the file, but I want to skip the operation on the first (header) line, though still keeping it there. 我正在使用fileinput
方法在文件中执行一些操作,但是我想跳过第一行(标题)上的操作,尽管仍将其保留在该行中。 I have tried using next(f)
and f.isfirstline()
, but they delete the header line. 我尝试使用next(f)
和f.isfirstline()
,但是它们删除了标题行。 I want to keep the header line intact, though not doing any operation on it. 我想保持标题行完整,尽管不对其执行任何操作。
with fileinput.input(inplace=True) as f:
#skip line
for line in f:
.
.
You can use iter
and skip it with next
: 您可以使用iter
并通过next
跳过它:
with fileinput.input(inplace=True) as f:
iterF = iter(f)
print next(iterF)#skipping computation but printing data
for line in iterF:
#...
This way you will just get the overhead of creating the iterator once, but will not create the indexes nor compute an if
in each iteration loop as in @JonathonReinhart solution (wich is also valid). 这样,您将只获得一次创建迭代器的开销,而不会像@JonathonReinhart解决方案一样在每个迭代循环中创建索引或计算if
(其中的方法也是有效的)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.