「2010 年 10 月 28 日」(または類似) の形式で指定された日付があります。ここで、月の完全な名前を短いバージョン (この場合は Oct) に変更したいと考えています。このために、私は辞書を用意しました:
_mapping = {'January': 'Jan',
'February': 'Feb',
'March': 'Mar',
'April': 'Apr',
'May': 'May',
'June': 'Jun',
'July': 'Jul',
'August': 'Aug',
'September': 'Sep',
'October': 'Oct',
'November': 'Nov',
'December': 'Dec'}
そして、置換が行われる方法で、私は次のように書きました:
def fetch(self, pno):
...
date = #get data (working fine)
for l, s in self._mapping.iteritems():
pubdate = date.replace(l, s)
print l + " -> " + pubdate #this is only for debug
(pubd_month, self.pubd_day, self.pubd_year) = pubdate.split(' ')
print pubd_month, self.pubd_day, self.pubd_year
print pubdate
実行結果は次のとおりです。
February -> October 28, 2008
October -> Oct 28, 2008
January -> October 28, 2008
April -> October 28, 2008
November -> October 28, 2008
March -> October 28, 2008
August -> October 28, 2008
May -> October 28, 2008
December -> October 28, 2008
June -> October 28, 2008
September -> October 28, 2008
July -> October 28, 2008
October
October 28, 2008
ご覧のとおり、October が見つかった場合は置換がうまくいくように見えますが、ループの外では完全な月の名前が再び取得されます。私は何を間違っていますか?
別の質問: これを行うためのより短い方法はありますか?