简体   繁体   English

如何使用python在excel中一起获取单元格的行值和列值(日期)

[英]How to get the row value and column value(date)together of a cell in excel using python

I have an excel pivot table of format:我有一个格式的 excel 数据透视表:

    Names 2/1/2010 3/1/2010 4/1/2010
    A       8                  
    B       4          5       7
    C       5          3
    D       6          6     

I need to get the names and date of the cells which are empty.我需要获取空单元格的名称和日期。 How can I do it?我该怎么做? I want the output as a list: [A:3/1/2010,4/1/2010] .我希望输出作为列表: [A:3/1/2010,4/1/2010]

Assuming format is same as above, Check this code snippet, you can use different python module to read excel sheet假设格式和上面一样,检查这个代码片段,你可以使用不同的python模块来读取excel表

import xlrd
def get_list_vals() :
    res = []
    path="C:/File_PATH.xlsx"
    wb=xlrd.open_workbook(path)
    sheet=wb.sheet_by_index(0)
    # Get rows from 2nd line
    for row in range(1, sheet.nrows) :
        temp = []
        for column in range (sheet.ncols) :
            val = sheet.cell_value(row,column)
            # get first column values like(A, B, C)
            if column == 0:
                temp.append(val)
                continue
            # if not first column, get the date data from row = 1
            elif val=="" :
                date_val = sheet.cell_value(0,column)
                temp.append(date_val)
        res.append(temp)
    return res

If you want specific format like [A : date1, date2] for thhis instead of temp = [] , you can append to string value如果你想要像 [A : date1, date2] 这样的特定格式而不是 temp = [] ,你可以附加到字符串值

temp = [] -->> temp = "" temp = [] -->> temp = ""

temp.append(val) --> temp += str(val) + ":" temp.append(val) --> temp += str(val) + ":"

temp.append(date_val) -->> temp + str(val) + "," temp.append(date_val) -->> temp + str(val) + ","

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 python 获取值的 excel 单元格/行号? - How to get excel cell/row number of a value using python? 如何使用python基于单元格值获取Excel单元格行和列 - How to get Excel cell row and col based on cell value using python 如何通过python中的单元格值获取列数或行数 - how to get column or row number by cell value in python 如何使用 python 在 excel 中获取特定列/单元格的水平值 - How do I get the value of a specific column / cell going horizontal in an excel using python 如何使用openpyxl在python中按单元格值获取列号 - how to get column number by cell value in python using openpyxl 如何使用Python提取Excel单元格的值? - How to extract the value of an excel cell using Python? 如何使用python更改一个Excel单元格值并使用公式读取与此单元格相关的列值? - How to change one Excel cell value using python and read a column values related to this cell with formula? 如何使用 Python Pandas 从 1 行中的特定列获取值? - How to get value from specific column in 1 row using Python Pandas? Python-使用列名获取 Excel 文件中特定单元格的值 - Python- get value of specific cell in Excel file using name of column Excel:如何在包含字符串的列中查找单元格并在另一列行中返回单元格的值? - Excel: How to find cell in a column that contains a string and return value of a cell in another column of row?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM