[英]Python: Looking for specific word or values in an excel file
所以我得到了两个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和电影名称。 唯一的问题是我不知道从哪里开始我是一个菜鸟编码者,我一直在努力学习,但是我不知道,如果你们可以帮助我并指出正确的方向,那将是惊人的。
谢谢
好的,因为您是菜鸟编码者,所以我将以一种实际上不需要任何库的简单方式向您解释。 我还要假设您正在交替使用电影标题和名称。
首先,您可以将excel文件转换为.csv
,代表用逗号分隔的文件(通过excel,只需另存为,然后选择csv即可。您也可以通过Google表格来完成此操作)。 什么是csv文件? 就像excel文件一样,不同之处在于每一行本身都是一行,并且不同的列之间用逗号隔开。 因此,csv中的前三行将是:
movieId,title,genres
1,Toy Story (1995),Adventure|Animation|Children|Comedy|Fantasy
2,Jumanji (1995),Adventure|Children|Fantasy
现在,.csv可以作为常规文件读取。 您应该逐行阅读它们。 这是用于此的python文档。 非常简单。
现在您已经将每一行都作为字符串,我们可以通过string.split()
命令将它们分割。 我们需要使用逗号作为分隔符来进行拆分,因为它是一个逗号分隔的文件。 到目前为止,我们的代码是这样的(我假设您将csv的不同行读入lines
数组):
lines = [...] # a list of strings which are the different lines of the csv
name_im_looking_for = "move you like" # the movie you're looking for
for(l in lines):
columns = l.split(',')
id = columns[0]
name = columns[1]
if(name.find(name_im_looking_for) != -1):
# this means the name you're looking for is within the 'name' col
print "id is", id, "and full name is", name
这只是一种粗略的方法,但是,如果您真的是编程新手,应该可以帮助您上路! 如果您有任何疑问,请随时提问(如果您真的很不错,并且只想知道如何使用openpyxl,请在问题中指定。)
这是在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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.