Created
August 28, 2010 12:41
-
-
Save emre/555091 to your computer and use it in GitHub Desktop.
Python icin turkce karakterlere uygun kucultme buyutlme fonksiyonlari
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| import re | |
| class TurkishTools(object): | |
| CHAR_MAP = { | |
| "to_upper": { | |
| u"ı": u"I", | |
| u"ö": u"Ö", | |
| u"ğ": u"Ğ", | |
| u"ü": u"Ü", | |
| u"ş": u"Ş", | |
| u"ç": u"Ç", | |
| u"i": u"İ", | |
| }, | |
| "to_lower": { | |
| u"I": u"ı", | |
| u"Ö": u"ö", | |
| u"Ğ": u"ğ", | |
| u"Ü": u"ü", | |
| u"Ş": u"ş", | |
| u"Ç": u"ç", | |
| u"İ": u"i", | |
| } | |
| } | |
| def __init__(self, text): | |
| self.text = unicode(text, "utf8") | |
| def to_upper(self): | |
| text = self.__charmap_handler(TurkishTools.CHAR_MAP.get("to_upper").items()) | |
| return self.text.upper() | |
| def to_lower(self): | |
| text = self.__charmap_handler(TurkishTools.CHAR_MAP.get("to_lower").items()) | |
| return self.text.lower() | |
| def ucfirst(self): | |
| temp = self.text[0] | |
| for key, value in TurkishTools.CHAR_MAP.get("to_upper").items(): | |
| temp = temp.replace(key, value) | |
| self.text = re.sub("^(.{1})", temp, self.text) | |
| return self.text.capitalize() | |
| def __charmap_handler(self, target): | |
| for key, value in target: | |
| self.text = self.text.replace(key, value) | |
| return self.text | |
| tr = TurkishTools("şırnak") | |
| print tr.to_upper() | |
| print tr.to_lower() | |
| print tr.ucfirst() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
sen ne kodmuşsun arkadaş, 20 kere geldim aldım kullandım. ellerime sağlık. +rep