如何重写' for循环'作为' while循环'在python?

时间:2017-03-29 22:47:42

标签: for-loop while-loop

这是循环:

data_xls = pandas.read_excel(excelPath, "FlowTimes" + gdbNum, index_col=None)
del data_xls['OBJECTID'] #Remove OBJECTID column
del data_xls['Shape_Length'] #Remove the Shape_Length column
data_xls.drop(data_xls.columns[0], axis =1) #Remove nameless first column

decimals = pandas.Series([0,2,0,2,0,2,0,2], index=['Length FT', 'Length MI','Flow Minutes - Mean Annual','Flow Hours - Mean Annual')

data_export = data_xls2.transpose() #Transpose columns to rows
data_export.reset_index()
data_export.columns = data_export.iloc[0]
data_export['Key Strategy Location'] = data_export.index #adds index names (row names) as last column

cols = data_export.columns.tolist() #Create list of columns
cols.insert(0, cols.pop(-1)) #Move column with index names (row names) to be the first column
data_export = data_export[cols]

os.remove(excelPath)

writer = pandas.ExcelWriter(excelPath + "x")
data_export.to_excel(writer, sheet_name = 'FlowTimes', startcol = 0, startrow = 0, header=False, index=False) #header = false removes index in the first row, index=false removes first column
workbook = writer.book
worksheet = writer.sheets['FlowTimes']
format1 = workbook.add_format()
format1.set_align('right')
format1.set_border(0)

format2 = workbook.add_format()
format2.set_bold()
format2.set_align('center')

worksheet.set_column("B:Z", 20, format1)
worksheet.set_row(0,None, format2)
worksheet.set_column("A:A", 30, format1)

writer.save()

这是我尝试重写为while循环:

for c in "Hi there":
     print(c.upper())

2 个答案:

答案 0 :(得分:0)

使用迭代器

phrase = "Hi there"
limit = len(phrase);
itery =0;
while itery<limit:
    print (phrase[itery].upper());
    itery+=1;

答案 1 :(得分:0)

我在这里看到了一些错误。

首先,您忘记在while语句的末尾使用冒号。

while len.phrase > 0:

其次,执行此任务的更有效方法如下:

while 1:

1将永远为真(在Python的情况下)并且它将执行无限循环,就像您之前的尝试似乎打算这样做。