This question already has an answer here:
I have an list which tells me which columns need to be converted from bytes into KB.
covert_to_kb_columns=[9, 10, 21, 22, 24]
I am having trouble implementing my code correctly. I want to check that if I am on the an 'idx'
number that matches one of the numbers in the covert_to_kb_columns
list than I should preform the calculation otherwise leave the number as is. The try/except
statement is for when there is no number in the file just a '', so I append a zero.
print covert_to_kb_columns
for idx, column_number in enumerate(columns_I_am_working_with):
print idx
#Check if I need to convert number from bytes to KB
if idx == [number for number in covert_to_kb_columns]:
print "Need to divide by 1024.0"
data_to_use.append("{:.1f}".format(float(row[column_number]) / 1024.0))
#Otherwise just append number as is
else:
try:
float(row[column_number])
data_to_use.append(row[column_number])
except ValueError:
data_to_use.append('0')
My print statements give me this result: (Note '.' is to indicate that all numbers were printed but I'm not writing them all 39 numbers out )
[9, 10, 21, 22, 24]
0
1
2
3
.
.
.
9
10
.
.
.
21
22
23
24
.
.
.
39
It seems that it never enters the print statement to perform the conversion. I think the issue is with the line
if idx == [number for number in covert_to_kb_columns]:
But I can't spot what I doing wrong
Aucun commentaire:
Enregistrer un commentaire