2016-11-19 5 views
-2

リストの以前の情報に応じてCSVファイルから情報を抽出するスクリプトを作成し、その情報を変更して最終出力リストを作成しました。スクリプトのバグが見つかりません

エラーは発生せず、最初の繰り返しと結果リストの最初のエントリでは問題なく動作します。ただし、他の反復の結果は空です。私は間に中間結果を印刷し、私はバグを見つけることができません。

# Definition to convert a date into the day of the year (did not want to use datetime module) 
def doy(month,day,mn,mz): 
    monthlist = [1,2,3,4,5,6,7,8,9,10,11,12] 
    s = 0 

    for m in monthlist: 
     #s = 0 
     if m < int(month): 
      j=0 
      for l in mn: 
       if m in l: 
        s += int(mz[j]) 
       else: 
        j += 1 
     elif m == int(month): 
      s += int(day) 

     else: 
      pass 

    return s 

# Lists of month for the definition above 
month29 = [2] 
month30 = [4,6,9,11] 
month31 = [1,3,5,7,8,10,12] 
md = [month29,month30,month31] 
mm = ["29","30","31"] 

#------------------------------------------------------------------------------------------- 

# Weather as string-Input 
Bsp="Wetter;leicht bewoelkt;sonnig/klare Nacht;sonnig/klare Nacht;mittel bewoelkt;stark bewoelkt;mittel bewoelkt;mittel bewoelkt;mittel bewoelkt;stark bewoelkt;mittel bewoelkt;mittel bewoelkt;stark bewoelkt leichter schneefall;stark bewoelkt leichter schneefall;stark bewoelkt leichter schneefall;stark bewoelkt leichter schneefall;stark bewoelkt leichter schneefall;stark bewoelkt leichter schneefall" 

# Date Input 
final_datum = ["31.03","31.03","31.03","31.03","1.04","1.04","1.04","1.04","2.04","2.04","2.04","2.04","3.04","3.04","3.04","3.04","4.04"] 

# Read the csv-file with radiation values 
fobj_rsunh_SEEG = open("H:\\radiation\\int_SEEG2.csv", "r") 

# Of every 4 identical dates, I just need the two in the middle (31.3,||31.3,31.3||,31,3,...) 
final_datum2 = [] 
for r in range(1,3): 
    fd = final_datum[r::4] 
    final_datum2.append(fd) 
final_datum3=[] 
for h in range(4): 
    for e in final_datum2: 
     final_datum3.append(e[h]) 

# Split the Bsp-string and do the same as with the dates (just get the second and third info of every four entries" 
iswr=Bsp 
iswr = iswr.split(";") 
iswr = iswr[1:] 

iswr2 = [] 
for rr in range(1,3): 
    isw = iswr[r::4] 
    iswr2.append(isw) 
iswr3=[] 
for hh in range(4): 
    for ee in iswr2: 
     iswr3.append(ee[hh]) 

# Define a list for the iteration below 
sbosf = ["stark bewoelkt","stark bewoelkt leichter schneefall","stark bewoelkt mittlerer schneefall","stark bewoelkt viel schneefal","mittel bewoelkt leichter schneefall","mittel_bewoelkt mittlerer schneefall","mittel_bewoelkt viel schneefall","leicht bewoelkt leichter schneefall","leicht bewoelkt mittlerer schneefall","leicht bewoelkt viel schneefall"] 
iswr_ht_list = [] 


k = 0       # k is a index of the iteration 

# depending on the entry of the iswr3-list, I get a different factor called "df" 
for i in iswr3: 
    if i == "sonnig/klare Nacht": 
     df = 1 

    elif i == "leicht bewoelkt": 
     df = 0.75 

    elif i == "mittel bewoelkt": 
     df = 0.5 

    elif i in sbosf: 
     df = 0.25 

    print "Weather = ",i 
    print "df = ",df 
    print "k = ",k 

    # the list of the date and the iswr3 have overlapping infos, that means iswr3[0] happens during final_datum3[0] -> therefore the index k 
    f = final_datum3[k] 
    f = f.split(".") 
    # split date to day and month and use the definition in the beginning of the cript to calculate the day of the year 
    day_f = f[0] 
    month_f = f[1] 
    day_of_year = doy(int(month_f),int(day_f),md,mm) 

    print "Day = ",day_f 
    print "Month = ",month_f 
    print "Day of the year = ",day_of_year 

    # empty lists of half-day values for later on 
    ht1_list = [] 
    ht2_list = [] 

    # read every line of csv file, split each line and define columns as variables: first column is the day of the year, second is time and third is radiation (rd) 
    for line in fobj_rsunh_SEEG: 
     line = line.split(",") 
     dy = line[0] 
     tm = line[1] 
     tmh = tm.split(".") 
     tmhh = tmh[0] 
     rd = line[2] 

     # if the day of the year in the csv-line (first column) is equal to the day of the year of my input list(iswr3), then append the radiation info of that line into list 1 or 2 depending of the half of the day (before or after 13 o`clock) 
     if int(dy) == day_of_year: 

      if int(tmhh) < 13: 
       ht1_list.append(float(rd)) 

      elif int(tmhh) > 12: 
       ht2_list.append(float(rd)) 

    # after the radiation info of one day(final_datum3) in the csv file was split into two lists (day-half 1 and day-half 2), those lists get modified with the factor "df" 
    iswr_ht1 = sum(ht1_list)*df 
    iswr_ht2 = sum(ht2_list)*df 
    print "Day Half 1: ", iswr_ht1 
    print "Day Half 2: ", iswr_ht2 

    # Add the two halfs together to get the radiation of the whole day 
    iswr_gt = iswr_ht1 + iswr_ht2 
    print "Integral of Day ",day_f,"amounts to ",iswr_gt 

    # append that info to the final result list 
    iswr_ht_list.append([day_f,iswr_ht1,iswr_ht2]) 

    # now add 1 to the index to do the same for the next final_datum3 and iswr3 value 
    k += 1 
    print "----------------------------------------------------------------" 

# print final result list 
print "List of Halfday-Values: ",iswr_ht_list 

私のCSVファイル(日、hour.minutes、放射線)のようになります。

91,12.00,270.534 
91,12.50,340,678 
91,13.00,764.987 
........ 

は、私は次のような結果を得る:

Weather = sonnig/klare Nacht 
df = 1 
k = 0 
Day = 31 
Month = 03 
Day of the year = 91 
Day Half 1: 10480.3028 
Day Half 2: 2699.23143 
Integral of Day 31 amounts to 13179.53423 
---------------------------------------------------------------- 
Weather = sonnig/klare Nacht 
df = 1 
k = 1 
Day = 31 
Month = 03 
Day of the year = 91 
Day Half 1: 0 
Day Half 2: 0 
Integral of Day 31 amounts to 0 
---------------------------------------------------------------- 
...... 
---------------------------------------------------------------- 
List of Halfday-Values: [['31', 10480.3028, 2699.23143], ['31', 0, 0], ['1', 0.0, 0.0], ['1', 0.0, 0.0], ['2', 0.0, 0.0], ['2', 0.0, 0.0], ['3', 0.0, 0.0], ['3', 0.0, 0.0]] 

あなたはその最初の反復を見ることができます正常に動作し、最後の結果リストにも含まれますが、他の反復には空の曜日の半分のリストがあり、したがって結果は一切ありません。

+3

これを[mcve]にカットします。その過程であなた自身の質問に答えることができます。 – jonrsharpe

+0

問題がどこにあるのか分かっていれば、最小限のコードしか得られないかもしれませんが、間違いがどこにあるのかを完全に検証できる例を与えるために、私はそれを打ち消すことはできません... Im sry –

+0

**あなたの仕事**は、どこに問題があるかを把握するために、これはアウトソースされたデバッグではありません。基礎を学ぶ:https://ericlippert.com/2014/03/05/how-to-debug-small-programs/。あなたがこのコードについて他の誰よりも多くを知っているにもかかわらず、それを分解できないなら、なぜ他の誰かが自由時間を過ごすことを期待していますか? – jonrsharpe

答えて

0

print()を使用してプログラムを確認します。デバッガの使い方を学ぶ。

Day Half 1が間違った結果を返した場合は、計算結果を確認してください。iswr_ht1 = sum(ht1_list)*dfprint df, ht1_list, ht2_listを使用すると、リストが空であることがわかります。

このリストが空である理由を確認したので、このリストが作成された場所を見つけて、もう一度print()を参照してください。どの部分が実行されるか(すなわち、print "i'm in 'if' statemant"

このリストはfobj_rsunh_SEEGに依存しますので、print fobj_rsunh_SEEGを使用してください。

これは、fobj_rsunh_SEEGがオープンファイルであることを示しています。しかし、このファイルを一度使用してすべてのデータを読み込んだので、このファイルの最後から読み込みました。同じデータを再度読み込むには、ファイルを開くか特殊なポインタをファイルの先頭に移動する必要があります。fobj_rsunh_SEEG.seek(0)

または単純にすべてのデータを1回だけファイルから読み込み、何度も同じデータを読み込みます。

このリストはif int(dy) == day_of_year:に依存するので、空のリストとDay Half 1 = 0が表示されることがあります。それが間違っている場合は、あなたのコードを再考する必要があります。

+0

本当にありがとうございました!わかった!私はちょうどループの中にcsvファイルの読みを入れて、ファイルの空の終わりを読むのではなく、もう一度それを読む必要があった。 –

+0

またはリストに一度読んでから、このリストを何度も使用してください。 – furas

関連する問題