Python:在Excel文件中查找特定的单词或值 [英] Python: Looking for specific word or values in an excel file

查看:0
本文介绍了Python:在Excel文件中查找特定的单词或值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我得到了两个EXCEL文件,如下例所示

movieId   title                 genres
1       Toy Story (1995)        Adventure|Animation|Children|Comedy|Fantasy
2       Jumanji   (1995)        Adventure|Children|Fantasy
3       Grumpier Old Men (1995) Comedy|Romance
4       Waiting to Exhale (1995)    Comedy|Drama|Romance
5       Father of the Bride Part II (1995)  Comedy

我想要做的事情是,当有人输入标题时,代码会找到MovieID和电影名称。唯一的问题是我不知道从哪里开始,我是一个新手程序员,我一直在尽我最大的努力学习,但我不知道,如果你们能帮助我,为我指明正确的方向,那就太棒了。

谢谢

推荐答案

下面是如何在Openpyxl中完成此操作的,因为您的问题中包含了Openpyxl标记:

import openpyxl as xl

workbook = xl.load_workbook(filename="test.xlsx")

title_column_name = "title"

# Get the active worksheet
ws = workbook.active

# The String we'll search for. You could prompt the user to provide
# this using python2's raw_input, oder python3's input function.
searchstring = "Grumpier"

# ws.rows[1:] means we'll skip the first row (the header row).
for row in ws.rows[1:]:
    # row[1] is the title column. string.find(str) returns -1
    # if the value was not found, or the index in the string if
    # the value was found.
    if row[1].value.find(searchstring) != -1:
        print("Found a matching row! MovieId={0}, Title={1}".format(row[0].value, row[1].value))

输出:

Found a matching row! MovieId=3, Title=Grumpier Old Men (1995)

这篇关于Python:在Excel文件中查找特定的单词或值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆