Skip to content

Instantly share code, notes, and snippets.

@shivkiyer
Created February 6, 2013 00:00
def csv_element(elem):
""" Takes the [row, column] input for a csv file
and given a human readable spreadsheet position. """
# Convert column numbers to alphabets
csv_col="A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"
csv_dict={}
csv_col_list=csv_col.split(" ")
for c1 in range(26):
csv_dict[c1]=csv_col_list[c1]
# Because row 0 doesn't exist on a
# spreadsheet
row=elem[0]+1
col=elem[1]
# Create a list of all the alphabets
# that a column will have
col_nos=[-1]
# On the run, an alphabet will
# have a remainder and a prefix
# This is essentially the first and
# second alphabet
prefix=0
remdr=col
# The alphabet that is to be found
col_count=0
while remdr-25>0:
# If the column>26, the first
# alphabet increments by 1
prefix+=1
remdr=remdr-26
if remdr<25:
if prefix>25:
# More than 2 alphabets
col_nos[col_count]=remdr
# The remainder takes the prefix
remdr=prefix-1
# The prefix is the third/next alphabet
prefix=0
# Add another element to the list
col_nos.append(-1)
col_count+=1
else:
# 2 alphabets only
col_nos.append(-1)
col_nos[-1]=prefix-1
col_nos[col_count]=remdr
col_letters=""
# The alphabets are backwards
for c1 in range(len(col_nos)-1, -1, -1):
col_letters=col_letters+csv_dict[col_nos[c1]]
csv_format=str(row)+col_letters
return csv_format
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment