Python 2.7 对于循环,请再运行一次
我将编写代码来打开一个文件,并从标题(6行)跳转,然后以0.005的间隔在输出文件打印时间。行数应与我打开的文件相同。 我拥有的文件有16行,但输出文件有17行。如何使for循环运行16 我试图定义打开文件的长度,但无法正常工作 提前谢谢 代码:Python 2.7 对于循环,请再运行一次,python-2.7,for-loop,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 m
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()
您只需更改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()
谢谢你的回答。我有这样的错误:不支持的操作数类型-:'file'和'int'