Python – Converting Ruby to Python

python, regex, ruby

I'm trying to convert my ruby script to python. I'm not very familiar with python so I am getting a TypeError.


Lease =, :renter)lease_list = []'input.txt').readlines.each do |line|  p, r = line.split(' - ')  lease_list <<'#', ''), r)end# sort by decimal valuelease_list.sort_by { |m|\d+/)[0].to_i }.each do |lease|  puts "\##{} - #{lease.renter}"end

import reclass Lease:  def __init__(self, renter=None, unit=None):    self.renter = renter = unitlease_list = []import syslines = open('input.txt', 'r')for line in lines:    l, m = line.split(' - ')    lease_list.append(Lease(l,m))lines.close()print lease_list.sort(key=lambda lease: re.split(r"\d+",

python error

Traceback (most recent call last):   File "", line 16, in<module>    print lease_list.sort(key=lambda str: re.split(r"\d+", str))   File "", line 16, in <lambda>    print lease_list.sort(key=lambda str: re.split(r"\d+", str))   File"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/",line 171, in split    return _compile(pattern, flags).split(string, maxsplit) TypeError: expected string or buffer

Best Solution

The problem is here:

print lease_list.sort(key=lambda str: re.split(r"\d+", str))

The str name [edit: see Question edit history], which you generally shouldn't use as a name, even as throwaways) which is assigned values contained in your list and consequently passed to re.split() is an object of type Lease:


This isn't accepted as an argument to re.split it likes munching on strs. hence the TypeError. Lease has two attributes which are strs after the line.split(' - '):

self.renter = = unit

Use one of these in re.split() (whichever is required for your use-case) with:

print lease_list.sort(key=lambda obj: re.split(r"\d+", obj.renter))


print lease_list.sort(key=lambda obj: re.split(r"\d+",

Forgot to mention, sorting a list with list.sort will return None since it sorts the list in place, printing the value here has no use.