[英]Python: How do I check for the values of each row and column?
I have this table, and I need to verify the column names first then the second row's values.我有这张表,我需要先验证列名,然后再验证第二行的值。 I'm new to Python and I tried to check on other site such as this: https://www.tutorialspoint.com/how-to-get-all-the-values-of-a-particular-row-based-on-a-condition-in-a-worksheet-in-selenium-with-python but do we have something like an automatic indexing for checking of values?
我是 Python 的新手,我试图在其他网站上进行检查,例如: https://www.tutorialspoint.com/how-to-get-all-the-values-of-a-particular-row-based- on-a-condition-in-a-worksheet-in-selenium-with-python但是我们是否有类似自动索引来检查值的东西? One approach is I can save the column names in an array and add assertion to check if they are equal.
一种方法是我可以将列名保存在一个数组中并添加断言以检查它们是否相等。 But how do I get the column names?
但是如何获取列名?
For the column names, I have to verify if it has the same data in the array:对于列名,我必须验证它是否在数组中具有相同的数据:
columnNames:["Column 1", "Column 2", "Column 3", "Column 4", "Column 5", "Column 6", "Column 7", "Column 8", "Column 9", "Column 10", "Column 11"]
For the value row I need to know how do I specify index but automatic?对于值行,我需要知道如何指定索引但自动? Or should I make it like
row[2].value
?或者我应该让它像
row[2].value
吗? But I'm not sure.但我不确定。 :/ I'm so confused...
:/ 我很混乱...
I only currently have this one:/我目前只有这个:/
columnNames: ["Column 1", "Column 2", "Column 3", "Column 4", "Column 5", "Column 6", "Column 7", "Column 8", "Column 9", "Column 10", "Column 11"]
activeWorksheet = excelTitle.active
for item in range(1,activeWorksheet.max_row+1):
global excelColumnNames
excelColumnNames.append(activeWorksheet.cell(row=item))
Its not clear what your ultimate requirement is.目前尚不清楚您的最终要求是什么。 Perhaps the following will help move in the right direction.
也许以下内容将有助于朝着正确的方向前进。
For the comparison you probably just want to use a python list as shown in the example code below as 'column_names'.为了进行比较,您可能只想使用 python 列表,如下面的示例代码所示为“column_names”。 You can then do a direct check if a Column Header is in the list and find the Index if that is necessary.
然后,您可以直接检查列 Header 是否在列表中,并在必要时查找索引。
The example code shows how you can iterate the header row and compare the Header text with that in the 'column_names' list and also compare the index position.示例代码显示了如何迭代 header 行并将 Header 文本与“column_names”列表中的文本进行比较,并比较索引 Z4757FE07FD492A8BE0EA6A760D683D6ZE。
You've noted that you want to "verify the column names first then the second row's values".您已经注意到您想要“先验证列名,然后验证第二行的值”。 For this you don't want to make two passes through the Excel cells, that'd be inefficient.
为此,您不想通过 Excel 单元进行两次传递,那将是低效的。 If possible, the verification and result of that check if it involves writing to the sheet should be achieved in the one pass.
如果可能,该检查的验证和结果是否涉及写入工作表应一次性完成。 As you have not included what verification is needed for the values I cannot comment on how to handle it here.
由于您没有包括值需要什么验证,我无法在此处评论如何处理它。
If you want to just extract the values for the headers to use outside Excel I have included creating a dictionary 'row_values', of the columns that verify on name and index.如果您只想提取标题的值以在 Excel 之外使用,我已经包括创建字典“row_values”,其中包含验证名称和索引的列。
from openpyxl import load_workbook
# List of column names to compare against
column_names =['Column 1', 'Column 2', 'Column 3', 'Column 4', 'Column 5', 'Column 6', 'Column 7', 'Column 8',
'Column 9', 'Column 10', 'Column 11']
# Open Excel workbook and sheet
wb = load_workbook(filename="foo.xlsx")
ws = wb['Sheet1']
row_values = {} # Dictionary for Column names and values that verify
for row in ws.iter_rows(max_col=ws.max_column, max_row=1):
for list_index, cell in enumerate(row):
c_value = cell.value
if c_value in column_names: # Check if the column name is in the list 'column_names'
# The following code is for columns where the name is in the 'column_names' list
row_index = column_names.index(c_value) # Get the index of the column name in the list 'column_names'
if row_index == list_index: # Check if the index match
print(f"Column name '{c_value}' is in the List at the correct Index {row_index}")
row_values[c_value] = cell.offset(row=1).value # If the column name and Index match add the column to the dictionary
else:
print(f"Column name '{c_value}' is in the List at an incorrect Index {row_index}") # If the index doesn't match this columns is not included in the dictionary
else:
# The following code is for columns where the name is not in the 'column_names' list
print(f"The Column name '{c_value}' is not in the List")
print("\nDictionary values:\n" + str(row_values))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.