在python中从excel表中

在python中从excel表中

本文介绍了在python中从excel表中查找值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Excel 工作表 (data.xlxs),其中包含超过 200 行的以下数据模式.

I have an excel sheet (data.xlxs) with the following pattern of data with more than 200 rows.

NS71282379_67698209    123456001
NS71282379_56698765    123456002
NS71282379_67698209    123456003
.
.
.

现在在我的脚本中,我试图找到 123456003 作为 NS71282379_67698209 的相应值.在我的脚本中,我想用 Excel 工作表中的值替换 123456003.我使用 xlrd 导入工作表,但没有找到任何可以轻松找到相应值的方法.我怎样才能聪明地做到这一点?

Now in my script, I am trying to find the corresponding value for 123456003 as NS71282379_67698209. In my script, I want to replace 123456003 with its value from the excel sheet.I used xlrd to import the sheet but haven't found any method which easily allows me to find the corresponding value.How can I do this smartly?

推荐答案

您可以按范围(sheet.nrows)迭代 Excel 工作表并根据行号获取行值.下面的脚本逐行迭代 Excel 工作表并打印出与值 123456003 匹配的行.您可以对其进行修改以满足您的要求

you can iterate the Excel sheet by range(sheet.nrows) and get row values based on the row number. The script below iterates the Excel sheet row by row and prints out the row that matches value 123456003. You can modify it to meet your requirement

$cat test.py

$cat test.py

import xlrd

def open_file(path):
    wb = xlrd.open_workbook(path)
    sheet = wb.sheet_by_index(0)


    for row_num in range(sheet.nrows):
        row_value = sheet.row_values(row_num)
        if row_value[1] == 123456003:
            print row_value

if __name__ == "__main__":
    path = "data.xlsx"
    open_file(path)

$python test.py

$python test.py

[u'NS71282379_67698209', 123456003.0]

这篇关于在python中从excel表中查找值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 03:52