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.

Now back to message at hand:

Message: 3
Date: Thu, 5 Nov 2009 21:34:31 -0600
From: Wayne Werner <[email protected]>
To: Katt <[email protected]>
Cc: tutor <[email protected]>
Subject: Re: [Tutor] Change a text string from a list and change it
into an integer number.(WinXP/py2.6.2/Beginner)

<snip>
Currently the above code does not work unless I change the "if" statement
to say:
"if check_year == "c".

It works correctly for me. Try modifying your code:

date = "cyear_11_05"
date2 = date.split("_")
check_year = date2[0]
print check_year

what does that do for you?
-Wayne

For me it prints to the screen the following : cyear

Message: 4
Date: Fri, 6 Nov 2009 09:06:02 +0530
From: Shashwat Anand <[email protected]>
To: Katt <[email protected]>
Cc: tutor <[email protected]>
Subject: Re: [Tutor] Change a text string from a list and change it
into an integer number.(WinXP/py2.6.2/Beginner)

What do you want to say exactly ?
is 'cyear' an integer ?
let's say date1 = "1984_11_05"

Here cyear is just part of a text string which would be in the following: "cyear_11_05"

My goal(which recent I accomplished with the help of Shashwat A.'s function) was:
1. Split the string into a list. [cyear,11,5]
2. If date0 == "cyear" then it would replace cyear with the current year based on the computer's date.
   Then it would change this information into an integer and return it.
3. If date0 != "cyear" it would then change the number into an integer and return the information.

find code that I used below at end of this message.

Then of course you can change it to an integer using following
list-comprehension,
date1 = "1984_11_05"
date1_list = [int(i) for i in date1.split("_")]
date1_list
[1984, 11, 5]
or alternatively,
date1_list_alternate=map(int,date1.split("_"))
date1_list_alternate
[1984, 11, 5]

I am still learning how to work with list comprehensions so I appreciate the multiple examples using the "for" and "map"
choices.

also your code seems to work on my system.
date = "cyear_11_05"
date2 = date.split("_")
check_year = date2[0]
if check_year == "cyear":
  year = localtime().tm_year
else:
  year = int(date2[0])
print year

Found that I had not imported localtime from time. It worked once I discovered this little oversight.


Message: 5
Date: Fri, 6 Nov 2009 09:27:46 +0530
From: Shashwat Anand <[email protected]>
To: Katt <[email protected]>
Cc: tutor <[email protected]>
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)

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  -  [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to