[Tutor] Tutor Digest, Vol 69, Issue 21 - Change a text string from a list and change it into an integer(WinXP/py2.6.2/Beginner)

Lie Ryan lie.1296 at gmail.com
Tue Nov 10 07:58:42 CET 2009


Katt wrote:
> First I want to thank the following people for your help: Wayne W., 
> Shashwat A., and Alan G.  I appreciate the input/criticism as I 
> continually learn more and more.
> 
> It seems that I am not sure if I am posting correctly to the right 
> thread and am not sure if it has to do with the fact that I am getting 
> the posts in a Digest manner.  Let me know if you have any suggestions.

If you're using the digest version, you should copy the subject line and 
simply add RE: in front of it (don't add "Tutor Digest, Vol. 69, Issue 
21 - ") to preserve threading in a threaded newsreader. If you want to 
change the subject line but keep the threading (often discussion drifts 
away from the original topic), you could copy the Message ID header 
(better idea, don't change the subject line unless you're using a 
threaded newsreaded)

>> Message: 5
>> Date: Fri, 6 Nov 2009 09:27:46 +0530
>> From: Shashwat Anand <anand.shashwat at gmail.com>
>> To: Katt <the_only_katala at verizon.net>
>> Cc: tutor <tutor at python.org>
>> Subject: Re: [Tutor] Change a text string from a list and change it
>> into an integer number.(WinXP/py2.6.2/Beginner)
>>
>> import time
>>
>> def katt(d):
>>    date0 = d.split("_")[0]
>>    if date0 == "cyear":
>>        return int(time.strftime("%Y"))
>>    else:
>>        return int(date0)
>>
>> print katt("cyear_11_05")
>> print katt("1984_11_05")
>>
>> http://codepad.org/RBjKmNcA
>>
>>
>> Hope this helps !
> 
> Thanks this helps.  I actually changed it a little so that I could 
> include it into another function rather than its own seperate function.
> 
> My code is as follows:
> 
> if year_check == "cyear":
>    year = int(strftime("%Y"))
> else:
>    year = int(year_check)
> if month_check == "cmonth":
>    month = int(strftime("%m"))
> else:
>    month = int(month_check)

an alternative could be to us str.replace:

from time import strftime

current_year = strftime("%Y")
current_month = strftime("%m")
current_day = strftme("%d")

def katt(date):
     date = date.replace("cyear", current_year)
     date = date.replace("cmonth", current_month)
     date = date.replace("cday", current_day)
     return map(int, date.split("_"))

> 
> I of course made sure to include the strftime in my import calls.  I may 
> change the int(strftime("%Y")) for localtime().tm_year because I think I 
> heard it returns the value as an integer, but will have to experiment.
> 
> Thanks again for the inspiration on this section of code.
> Thanks again to all.
> 
> Katt
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
> 



More information about the Tutor mailing list