- 1 year ago 66
Python Question

Script Bug not findable

I wrote a script to extract info from a CSV file, depending on former info in lists, modify that info and create a final output list.

There are no errors occurring and it works perfectly fine for the first iteration and the first entry of the result list. However, the results of the other iterations are empty. I printed interim results in between and I just cannot find the bug.

# 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):
for l in mn:
if m in l:
s += int(mz[j])
j += 1
elif m == int(month):
s += int(day)


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]
for h in range(4):
for e in final_datum2:

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

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

# 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:

elif int(tmhh) > 12:

# 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

# 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

My csv-file looks like (day,hour.minutes,radiation):


I get the following results:

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]]

you can see that the first iteration works fine and is also in the last result list, but the other iterations have empty day-half-lists and therefore no result integral

Answer Source

Use print() to check program. Or learn how to use debuger.

If Day Half 1 gives wrong result then check calculations which gives this results - iswr_ht1 = sum(ht1_list)*df . Use print df, ht1_list, ht2_list and you see that lists are empty.

Now you have check why this list are empty so you have to find pleace where this lists are created and use again print() to see: values in variables; which parts of code are executed (ie. print "i'm in 'if' statemant"), etc.

This list depends on fobj_rsunh_SEEG so use print fobj_rsunh_SEEG.

This shows that fobj_rsunh_SEEG is open file. But you used this file once and read all data so now you read from end of this file. To read again the same data you have to reopen file or move special pointer to the beginning of file -

Or simple first read from file all data only once and use the same data many times (without reading).

This lists depends on if int(dy) == day_of_year: so sometimes it gives empty lists and Day Half 1 = 0. If it is incorrect then you have to rethink your code.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download