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

2024-09-30 08:27:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我的职位如下:

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

我想在循环中附加从xlsx文件中读取的值,这样我就可以将North1作为一个excel文件的B列,Center1作为另一个excel文件的B列,以此类推。这就是我目前所做的:

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)

如何使最后一部分进入循环中?你知道吗


Tags: 文件thepathinforvaluepositionexcel
1条回答
网友
1楼 · 发布于 2024-09-30 08:27:36

将第二个循环再缩进4个空格,这将使其进入“for i in sheets\u 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)

相关问题 更多 >

    热门问题