从文件中读取,存储值和数学运算

时间:2011-10-17 17:14:15

标签: python file-io

我正在尝试从包含城市名称,里程表读数以及剩余燃料量的文件中读取。文本文件的内容是:

Kennewick 97840.4 40.0<br>
Benton_City 97861.3 37.6<br>
Prosser 97878.2 35.9<br>
Grandview 97886.5 35.0<br>
Sunnyside 97894.6 34.1<br>
Granger 97904.6 33.1<br>
Toppenish 97913.0 32.2<br>
Wapato 97921.7 31.3<br>
Union_Gap 97931.6 30.4<br>
Yakima 97935.2 30.0<br>
Selah 97939.4 29.6<br>
Ellensburg 97972.7 26.0<br>
Thorp 97981.1 25.1<br>
Rosyln 98002.8 22.6<br>
Easton 98016.1 21.1<br>
North_Bend 98058.1 16.9<br>
Preston 98067.7 16.0<br>
Issaquah 98073.6 15.4<br>
East_Gate 98082.9 14.4<br>
Mercer_Island 98088.5 13.7<br>
Seattle 98095.4 13.0<br>

部分分配是从上一次读取中减去当前的读取(存储在变量里程中的两个值)。

def BusRoute():
    path = "C:\\"
    ext = ".txt"

    lines = ""
    stopname = ""
    miles = 0
    prevmiles = 0
    milest = 0
    fuel = 0.0
    mpg = 0.0
    i = 0

    filename = input("Please enter the file name. The file path [C:\...] and extension [*.txt] is not required: ")

    inFile = open(path + filename + ext, "r")

    for lines in inFile:
        stopname, miles, fuel = lines.split(" ")

        miles = float(miles)
        fuel = float(fuel)

        prevmiles = miles

        milest = miles - prevmiles

        miles += 1

        print("Since last stop:", float(milest), "miles")

我的问题是,我正在尝试将miles的最后读取值存储在名为prevmiles的变量中,然后从CURRENT中减去prevmiles(最后读取的里程数)读取miles,但它会从当前miles中减去当前miles,显然会使我想要的值打印出0.0。

4 个答案:

答案 0 :(得分:2)

你有

prevmiles = miles

milest = miles - prevmiles

保证里程数为0.尝试只切换这两行的顺序。

答案 1 :(得分:1)

这里有两件事:

  1. 您需要使用文件的第一行初始化stopnameprevmilesfuel
  2. 您需要交换将miles分配给prevmiles的两行。
  3. 这是更新后的功能:

    def BusRoute():
        path = "C:\\"
        ext = ".txt"
    
        lines = ""
        stopname = ""
        miles = 0
        prevmiles = 0
        milest = 0
        fuel = 0.0
        mpg = 0.0
        i = 0
    
        filename = input("Please enter the file name (no extension)").strip()
    
        inFile = open(path + filename + ext, "r")
    
        stopname, prevmiles, fuel = inFile.readline().split()
        prevmiles = float(prevmiles)
        fuel = float(fuel)
    
        for lines in inFile:
            stopname, miles, fuel = lines.split(" ")
    
            miles = float(miles)
            fuel = float(fuel)
    
            milest = miles - prevmiles
    
            prevmiles = miles
    
            miles += 1
    
            print("Since last stop:", float(milest), "miles")
    

答案 2 :(得分:0)

您应该只更改操作的顺序。首先进行减法,然后为前一个值赋值。

答案 3 :(得分:0)

我切换了两条线,得到了我认为你正在寻找的东西。

prevmiles = miles

milest = miles - prevmiles

注意 - 此代码适用于Python 2.5.4,因此语法会在几个部分中发生变化,并且由于行末的第一部分,因为燃料的浮点数会产生错误,所以我对此进行了评论。

def BusRoute():
    path = "C:\\"
    ext = ".txt"

    lines = ""
    stopname = ""
    miles = 0
    prevmiles = 0
    milest = 0
    fuel = 0.0
    mpg = 0.0
    i = 0

    filename = raw_input("Please enter the file name. The file path [C:\...] and extension [*.txt] is not required: ") #raw_input for text in Python 2.5

    inFile = open(path + filename + ext, "r")

    for lines in inFile:
        stopname, miles, fuel = lines.split(" ")

        miles = float(miles)
        ##fuel = float(fuel) (Gives me an error message)

        milest = miles - prevmiles

        prevmiles = miles            

        miles += 1

        print "Since last stop:", float(milest), "miles" #Print syntax is different in Python 2.5

BusRoute()

给出输出:

Please enter the file name. The file path [C:\...] and extension [*.txt] is not required: name
Since last stop: 97840.4 miles
Since last stop: 20.9 miles
Since last stop: 16.9 miles
Since last stop: 8.3 miles
Since last stop: 8.10000000001 miles
Since last stop: 10.0 miles
Since last stop: 8.39999999999 miles
Since last stop: 8.7 miles
Since last stop: 9.90000000001 miles
Since last stop: 3.59999999999 miles
Since last stop: 4.2 miles
Since last stop: 33.3 miles
Since last stop: 8.40000000001 miles
Since last stop: 21.7 miles
Since last stop: 13.3 miles
Since last stop: 42.0 miles
Since last stop: 9.59999999999 miles
Since last stop: 5.90000000001 miles
Since last stop: 9.29999999999 miles
Since last stop: 5.60000000001 miles
Since last stop: 6.89999999999 miles

为什么不尝试在循环中打印文件里程以确保获得正确的值?