I'm trying to convert my ruby script to python. I'm not very familiar with python so I am getting a TypeError.
printer.rb
Lease = Struct.new(:property, :renter)
lease_list = []
File.open('input.txt').readlines.each do |line|
p, r = line.split(' - ')
lease_list << Lease.new(p.tr('#', ''), r)
end
# sort by decimal value
lease_list.sort_by { |m| m.property.scan(/\d+/)[0].to_i }.each do |lease|
puts "\##{lease.property} - #{lease.renter}"
end
printer.py
import re
class Lease:
def __init__(self, renter=None, unit=None):
self.renter = renter
self.property = unit
lease_list = []
import sys
lines = 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+", lease.property))
python error
Traceback (most recent call last): File "printer.py", line 16, in
<module>
print lease_list.sort(key=lambda str: re.split(r"\d+", str)) File "printer.py", 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/re.py",
line 171, in split
return _compile(pattern, flags).split(string, maxsplit) TypeError: expected string or buffer
TypeError. A syntax error shows up asSyntaxError. And yes, there is a significant difference.lease.unitwithlease.propertyon the last line