Skip to content

Instantly share code, notes, and snippets.

@berlotto
Created December 2, 2016 21:40
Show Gist options
  • Select an option

  • Save berlotto/25b8cf21bc607daf4b3e5498a68b7a55 to your computer and use it in GitHub Desktop.

Select an option

Save berlotto/25b8cf21bc607daf4b3e5498a68b7a55 to your computer and use it in GitHub Desktop.
Create a dictionary containing the excel columns name
import string
from copy import deepcopy
def excel_columns(max_lenght=None):
"""
This method maps the excel columns names with a simple index,
Ex:
{0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E', ...
26: 'AA', 27: 'AB', 28: 'AC', 29: 'AD', 30: 'AE', ...
1378: 'AAA', 1379: 'AAB', 1380: 'AAC', 1381: 'AAD', 1382: 'AAE',... }
Returns: dict
"""
class BrakeAllLoops(Exception):
pass
letras = [x for x in string.uppercase]
result = [x for x in string.uppercase]
if not max_lenght:
# This number is for the result to have from A to ZZZ
max_lenght = 18954
else:
try:
max_lenght = int(max_lenght)
except ValueError:
raise TypeError("max_length parameter must be a integer number")
# 'deepcopy' is for the loop not to be changed when adding data to
# the result variable
copia = deepcopy(result)
try:
while True:
for atual in copia:
for letra in letras:
result.append(atual + letra)
if len(result) >= max_lenght:
raise BrakeAllLoops()
copia = deepcopy(result)
except BrakeAllLoops:
pass
return dict(enumerate(result))
print excel_columns()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment