Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python while循环以搜索文件中的最高值_Python_Loops_Max - Fatal编程技术网

Python while循环以搜索文件中的最高值

Python while循环以搜索文件中的最高值,python,loops,max,Python,Loops,Max,我正在写一个循环来搜索excel文件。关键是检查读取行中的最高值。这是到目前为止我的代码 def maxwages(): leave = false max = 0 city = '' #city for later printing fo = open(path,'r') #open file while leave == false: line = fo.readline() #read file newline = line.spli

我正在写一个循环来搜索excel文件。关键是检查读取行中的最高值。这是到目前为止我的代码

def maxwages():
   leave = false
   max = 0
   city = '' #city for later printing
   fo = open(path,'r') #open file
   while leave == false: 
      line = fo.readline() #read file
      newline = line.split(',')
      check = newline[len(newline)-1] #the value is the last element in the line
      if check > max: #if the checked value is higher than previous max
        max = newline[len(newline)-1] #assign new max value with checked value
        city = newline[2] #city name
        print max, city #seeing what's going on
      if fo is None:
        leave = true
   print city,'Has the highest wages at',max,'dollars'
      fo.close
我当前的输出是

"TotalWages"
"City"
1089095041
"WESTWOOD"
325436960
"WOODCLIFF LAKE"
401312434
"WHITEHOUSE STATION"
528315021
"WOODBRIDGE"
896273759
"WYCKOFF"
924776075
"BRONX"
97578251
"BRONX"
98754584
"AVON"
9999157
"BUZZARDS BAY"
我不确定max的值为什么会下降。我必须阅读的文件是42000行数据,我无法集中精力获取正确的最大值。抱歉,如果有任何模糊的地方,这是我在网站上的第一个问题和帖子


另一次编辑添加了我正在搜索的数据,到目前为止,回复非常有用

您可以使用不同的方法来解决这个问题

with open(path, "r") as f:
    lines = f.readlines()

    # Next line will create a list of tuples. Each tuple will be (value, city), assuming value is at position -1 of your line and city at position 2
    values_cities = [(int(line.split(',')[-1]), line.split(',')[2]) for line in lines]

    max_pair = max(values_cities) # It will find the max of every tuple (the first element, which is the value)

您需要将字符串转换为数字:
check=int(换行符[-1])
我将尝试在这里添加一行,我必须看看它是如何格式化的。编辑它看起来很糟糕我不确定它是否有用
7675标准WESTWOOD NJ PRIMARY 40.98-74.03 NA-US-NJ-WESTWOOD FALSE 13245 24083 1089095041
,所以我放弃了试图在评论中发布的尝试,并在我的数据中添加了一个谷歌表链接,不确定这是否有用这是一个有趣的解决方案,我还在学习python,不知道我能做到这一点,谢谢你的建议!