2016-12-15 50 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