python 根據excel單元格內容獲取該單元格所在的行號


python 環境:Python 2.7.16

需要安裝:pandas/xlrd (pip2 install pandas/ pip2 install xlrd)

import pandas as pd

def find_row(num_value,file_name):
    """
    Returns the row number based on the value of the specified cell
    """
    demo_df = pd.read_excel(file_name)
    for indexs in demo_df.index:
        for i in range(len(demo_df.loc[indexs].values)):
            if (str(demo_df.loc[indexs].values[i]) == num_value):
                row = str(indexs+2).rstrip('L')
                return row

row_num = find_row('103.03','test.xlsx')
print(row_num)

 

以上代碼實現:從‘test.xlsx'中獲取單元格’103.03‘所在的行號(注意:如果存在多個單元格的value是’103.03‘,只返回第一次讀到的行號)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM