簡體   English   中英

通過標題循環列值

[英]Looping column values through header

我在左側有一個帶有隨機日期的列,以及一個帶有日期的標題行(日歷)。

我試圖循環列值,並為每個值查找標題值,一旦在標題中找到值A,我想打印在標題中找到的值A的坐標。

        01.01.2019 | 02.01.2019 | 03.01.2019 |.... | 02.02.2019 |

2019年1月1日
2019年2月2日
2019年2月15日

該代碼應選擇01.01.2019(左列)並循環瀏覽標題日期。 找到01.01.2019的對應方后,應打印標題坐標(B1)。 依此類推,剩余日期為02.02和15.02。

我試着用ws.rows和ws.iter_cols循環。

**for row in ws.rows:
 if row[1].value == "28.06.2019":
      print("Found it vertically, value is:", row[1].value)
      #for cell in row:
      #    print(cell.value, end=" ")
      print("Vertical Value:", row[1].value, "Coordinates:", row[1].coordinate)
      target.append(row[1].coordinate)
      break
print("TARGET1:", target)
for col in ws.iter_cols(min_row=1, max_col=30, max_row=2):
     for cell in col:
          if cell.value =="28.06.2019":
               print("Horizontal Value:", cell.value, "Coordinates:", cell.coordinate)
               target.append(cell.coordinate)
          else:
               break**

我建議您建立標題的字典並使用此查找。 類似以下內容應有所幫助。

header = {c.value:c.coordinate for c in ws[1]}
for cell in ws['A']:
    print("{0} maps to {1}".format(cell.coordinate, header[cell.value])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM