【问题标题】:Rename header column names in Excel files using Python?使用 Python 重命名 Excel 文件中的标题列名称?
【发布时间】:2022-06-22 23:14:35
【问题描述】:

我是 python 新手,我想重命名 Excel 文件中的标题列名称。

在输入文件中,所有内容都是大写字母。 我想将其转换为标题大小写(首字母大写,其余小写)。

示例

例如从MKTMarket

【问题讨论】:

标签: python excel


【解决方案1】:

要在 Market 中更改 MKT,您将不得不使用字典,因为程序很难将 MKT 翻译成 Market。

aliases = {"MKT": "Market", "US": "United States"} # You can expand the dict.
# Then try something to get all the headers in a list
headers = [put your headers in here somehow]
for index, value in enumerate(headers):
    if header in aliases.keys():
        header = aliases[header]
        headers[index] = header

至于更改大写:

str = "ANY STRING"
str = str.lower().capitalize()
        

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-23
    • 1970-01-01
    • 1970-01-01
    • 2015-04-17
    • 1970-01-01
    • 2018-12-26
    • 1970-01-01
    相关资源
    最近更新 更多