对于循环运行一次

时间:2015-12-12 10:59:15

标签: python-2.7 for-loop

我打算编写代码打开文件并从标题(6行)跳转,然后在输出文件打印时间内以0.005间隔跳转。行数应与我打开的文件相同。 我拥有的文件,有16行,但输出文件有17.如何使for循环运行16?

我尝试定义已打开文件的长度,但无效。

提前感谢。

代码:

import time
from tkFileDialog import *
import Tkinter as T
import easygui as Ea
import pandas as pd
import numpy as N
import math as M   




name0= 'file.txt'



with open(name0, "r") as f:



    f.next() 
    f.next()
    f.next() 
    f.next()
    f.next() 
    f.next()

    fintime = fintime - 0.005

    for line in (f):



        fintime = fintime + 0.005

        numbers_str = line.split()

        numbers_float = [float(x) for x in ((numbers_str))]                       







        fName=name0+'new.txt'
        w1 = open(fName, "a")  
        w1.write("%s\n" % ((str(format(fintime, '.3f'))) ))            
        w1.close()

1 个答案:

答案 0 :(得分:0)

您可以更改for循环并手动删除额外的一圈,如下所示:

for line in (f-1):

    fintime = fintime + 0.005
    numbers_str = line.split()
    numbers_float = [float(x) for x in [(numbers_str)-1]                       

    fName=name0+'new.txt'
    w1 = open(fName, "a")  
    w1.write("%s\n" % ((str(format(fintime, '.3f'))) ))            
    w1.close()