2015-12-12 170 views
-1

我打算編寫代碼打開一個文件並從頭文件(6行)跳轉,然後在輸出文件打印時間內以0.005的間隔跳轉。行數應該與我打開的文件相同。 我有的文件有16行但輸出文件有17個。我怎樣才能讓for循環運行16?For循環運行一次更多

我嘗試定義打開文件的長度,但沒有工作。

在此先感謝。

代碼:

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() 

回答

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() 
+0

感謝answer.I有這樣的錯誤:_unsupported操作數類型( s)爲 - :'file'和'int'_ –