Spreadsheet Column to Index Conversion in Python

If you’ve ever had to dig down into specific columns of a spreadsheet with > Z columns you probably know how annoying it is to try to convert something like ‘AQ’ to the appropriate index (e.g. row[42]). Here’s a quick one-liner to convert a spreadsheet letter-based column to the equivalent zero-based index.


col2num = lambda col: reduce(lambda x, y: x*26 + y, [ord(c.upper()) – ord('A') + 1 for c in col])-1

view raw

col2num.py

hosted with ❤ by GitHub

For example:

  • col2num(‘A’) -> 0

  • col2num(‘Z’) -> 25

  • col2num(‘BC’) -> 54

Word Scrambling in Python

To get this new blog started I’m poaching an old post I made about scrambling words while retaining legibility. In the time since I originally posted this I found the source.

I was recently reading the Lucky Basartd page on the Stone Brewery website and I remembered an (unverified) study claiming “scrambled words are legible as long as first and last letters are in place.” For grins, I whipped up a Python script to scramble a word/sentence.


#!/usr/bin/python
# -*- coding: utf-8 -*-
def scramble(unscrambled):
'''
Scrambles the word(s) in unscrambled such that the first and last letter remain the same,
but the inner letters are scrambled. Preserves the punctuation.
See also: http://science.slashdot.org/story/03/09/15/2227256/can-you-raed-tihs
'''
import string, random, re
splitter = re.compile(r'\s')
words = splitter.split(u''.join(ch for ch in unscrambled if ch not in set(string.punctuation)))
for word in words:
len_ = len(word)
if len_ < 4: continue
if len_ == 4:
scrambled = u'%c%c%c%c' % (word[0], word[2], word[1], word[3])
else:
mid = list(word[1:-1])
random.shuffle(mid)
scrambled = u'%c%s%c' % (word[0], ''.join(mid), word[-1])
unscrambled = unscrambled.replace(word, scrambled, 1)
return unscrambled
if __name__ == '__main__':
print scramble(u'Indeed, here be some scrÖmbled words!')

view raw

scramble.py

hosted with ❤ by GitHub

Featured image via mj ecker