2015-03-19 51 views
-3

UPDATE:Python的Excel中,如果條件爲真打印下一行T + 1

我想蟒蛇打印在Excel中如T + 1的下一行文件到Python的輸出窗口,當條件滿足。我認爲,問題出在代碼

print "FA",row[0], ','.join(row[1:]) 

這將打印本一天,我需要蟒蛇打印第二天的這一部分(T + 1)

我試圖print "FA",row[0], ','.join(row[1:]).nextline() 但不起作用

例如 可以說Excel文件有

35F 1,0,1,0,1,1,1 
66F 1,0,1,0,0,0,0 

35F 1,0,1,0,1,1,1<-- at this moment python prints this part 

66F 1,0,1,0,0,0,0<--- **But I want python to print this {66F 1,0,1,0,0,0,0} this is the t+1** 

而且,這裏是完整的代碼

import csv 

with open('2015weather.csv', 'r') as file1: 
    val = list(csv.reader(file1))[3] 


with open('FAsince1900.csv', 'r') as file2: 
    reader = csv.reader(file2) 
    reader.next() # this skips the first row of the file 
    # this iteration will start from the second row of file2.csv 
    for row in reader: 
     if row[1:] == val: 
      print print "FA",row[0], ','.join(row[1:]) 

更新2: this is goal

+0

你想兩個打印語句? – Levon 2015-03-19 12:02:54

+0

我想你可能想要像http://stackoverflow.com/q/5434891/3001761這樣的東西,但目前還不清楚 - 目前你的問題到底是什麼? – jonrsharpe 2015-03-19 12:03:42

+0

我已經更新了這個問題,希望這有助於 – 2015-03-19 19:08:01

回答

1

怎麼是這樣的:

conditionMet = False 
for row in reader: 
    if conditionMet == True: 
     print "FA",row[0], ','.join(row[1:]) 
     conditionMet = False # or break if you know you only need at most one line 
    if row[1:] == val: 
     conditionMet = True