-1

I am new to python and I want to rename the header column names in an Excel file.

In the input file all the content is in capital letters. I want to convert that into title-case (first letter capital and rest small).

Example

For example from MKT to Market:

Input file picture

hc_dev
  • 8,389
  • 1
  • 26
  • 38
  • 1
    What did you try? E.g. using [OpenPyxl](https://stackoverflow.com/questions/34296132/how-to-use-field-name-or-column-header-in-openpyxl) ..please share your code-attempt as [example]. – hc_dev Jun 22 '22 at 15:07

1 Answers1

0

To change MKT in Market you're gonna have to use dictionnaries as there is no easy for the program to translate MKT into 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

As for changing the capitals:

str = "ANY STRING"
str = str.lower().capitalize()
        
Tirterra
  • 579
  • 2
  • 4
  • 14
  • Okay but I want to change all the column name in one go then – Kanan Parmar Jun 22 '22 at 15:24
  • could you edit your post and show us how your database is setup and what we have to work with ? Such as example lists or dicts containing your data. Basically the input we have and an example of the output you want. – Tirterra Jun 22 '22 at 15:39