如何在循环中的字符串变量中追加?

时间:2018-04-26 08:06:08

标签: python string interaction

我有列表位置:

position = ["North", "Center", "South"]

我想在循环中追加我从xlsx文件读取的值,所以我将North1作为一个excel文件中的B列,来自另一个excel文件的Center1,依此类推。这是我到目前为止所做的:

for p in position:

    for i in sheets_heights:
        sheetHeights = openpyxl.load_workbook(os.path.join(path,filename), data_only=True)
        heights = sheetHeights.get_sheet_by_name("Sheet1")

        North1=[]
        North2=[]
        North3=[] #here I wanted to create North1, Center1, South1 as a looping...
        North4=[]

#here I wanted to get the variables created on the looping above and append the column values from excel

    for row in range (2, heights.max_row+1):
        Time.append(heights['A' + str(row)].value)
        p.__add__North1.append(heights['B' + str(row)].value) #test
        North2.append(heights['C' + str(row)].value)
        North3.append(heights['D' + str(row)].value)
        North4.append(heights['E' + str(row)].value)

如何让最后一部分进入循环?

1 个答案:

答案 0 :(得分:0)

将第二个循环缩进4个空格,这将把它放在"对于i中的sheet_heights"循环。

编辑:生成的代码为:

North1=[]
North2=[]
North3=[] #here I wanted to create North1, Center1, South1 as a looping...
North4=[]

for p in position:

    for i in sheets_heights:
        sheetHeights = openpyxl.load_workbook(os.path.join(path,filename), data_only=True)
        heights = sheetHeights.get_sheet_by_name("Sheet1")

        #here I wanted to get the variables created on the looping above and append the column values from excel

        for row in range (2, heights.max_row+1):
            Time.append(heights['A' + str(row)].value)
            North1.append(heights['B' + str(row)].value)
            North2.append(heights['C' + str(row)].value)
            North3.append(heights['D' + str(row)].value)
            North4.append(heights['E' + str(row)].value)