2016-12-15 54 views
0

我可以在Python中使用with open來遍歷逗號9月份的文本文件,但我希望增加更多複雜程度。 我需要返回找到字符串的行的值爲index[0]使用Python以CSV格式返回給定行的索引值[0]值

例如,我有一個包含一個文本文件:

00:00,19.90,990.49,59.16,11.78,No 
01:00,19.92.991.00,59.75,11.90,Yes 
02:00,19.76,991.21,58.87,10.95,No 
03:00,19.34,989.97,57.00,10.64,Yes 

現在我使用:

MaxTemp = -float('inf') 
MinTemp = +float('inf') 
with open (YdPath + yFileDate + '.txt', 'r') as DailyData: 
    for lines in DailyData: 
     temp = float(lines.strip().split(',')[1]) 
     if MaxTemp < temp: 
      MaxTemp = temp 
     if MinTemp > temp: 
      MinTemp = temp 

輸出將是:

MaxTemp = 19.92 
MinTemp = 19.34 
現在

但我希望得到與這些條目相關的index[0]值,即

MaxTemp需要找到的行中的條目在19.92開始的01:00 & index[0]顯示爲這樣的,使用可變tTime作爲index[0]值:

print 'The Max Temp was ' + MaxTemp + ' recorded at ' + tTime 

感謝您尋找

UPDATE

感謝去Henry Heath尋求幫助&指針。

所需2時的變量,如MaxTemp & MinTemp使用tTime這裏返回完全相同的時間爲正確的工作代碼:

MaxTemp = -float('inf') 
MinTemp = +float('inf') 

with open (YdPath + yFileDate + '.txt', 'r') as DailyData: 
    for line in DailyData: 
     line = line.strip().split(',') 
     temp = float(line[1]) 
     if MaxTemp < temp: 
      MaxTemp = temp 
      MXtTime = line[0] 
     if MinTemp > temp: 
      MinTemp = temp 
      MNtTime = line[0] 

MaxTemps = '%.1f' %MaxTemp 
MinTemps = '%.1f' %MinTemp 

print('The Max Temp was ' + MaxTemps + ' recorded at ' + MXtTime) 
print('The Min Temp was ' + MinTemps + ' recorded at ' + MNtTime) 

回答

1

如果使用csv

MaxTemp = -float('inf') 
MinTemp = +float('inf') 
with open (YdPath + yFileDate + '.txt', 'r') as DailyData: 
    for line in DailyData: 
     line = line.strip().split(',') 
     temp = float(line[1]) 
     if MaxTemp < temp: 
      MaxTemp = temp 
      tTime = line[0] 
     if MinTemp > temp: 
      MinTemp = temp 
這可能是更容易
+0

謝謝,你的回答從一個小問題,拋開工作在時間兩個'MaxTemp'&'MinTemp'被顯示爲相同的,但它讓我正確的道路上。我已添加了反映此問題的更新,儘管檢查您的答案是正確的。非常感謝。 – 1cm69

0

以下是@ henry-heath的建議:

import csv 
from decimal import Decimal 

# n.b., this implementation uses a sort for the brevity of 
# implementation. However, the original iterative approach 
# will be much faster (O(n) compared to O(n log n)) than 
# the version below 
with open (YdPath + yFileDate + '.txt', 'r') as f: 
    reader = csv.reader(f) 
    lines = [line for line in reader] 
    lines.sort(key=lambda x: Decimal(x[1])) 
    print 'The Max Temp was ' + lines[-1][1] + ' recorded at ' + lines[-1][0] 
    print 'The Min Temp was ' + lines[0][1] + ' recorded at ' + lines[0][0] 

迭代版本:

import csv 
from decimal import Decimal 

with open (YdPath + yFileDate + '.txt', 'r') as f: 
    reader = csv.reader(f) 
    line = reader.next() 
    line[1] = Decimal(line[1]) 
    min_temp, max_temp = line, line 
    for x in reader: 
     x[1] = Decimal(x[1]) 
     if x[1] < min_temp[1]: min_temp = x 
     if x[1] > max_temp[1]: max_temp = x  
    print 'The Max Temp was ' + max_temp[1] + ' recorded at ' + max_temp[0] 
    print 'The Min Temp was ' + min_temp[1] + ' recorded at ' + min_temp[0] 
+0

這種方法看起來很有前途,也比較容易閱讀,我會試試看,謝謝。 – 1cm69