[Tutor] Change a text string from a list and change it into an integer number.(WinXP/py2.6.2/Beginner)

Shashwat Anand anand.shashwat at gmail.com
Fri Nov 6 04:36:02 CET 2009


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

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]


also your code seems to work on my system.

On Fri, Nov 6, 2009 at 8:14 AM, Katt <the_only_katala at verizon.net> wrote:

> Hello all,
>
> I was wondering if it was possible to split a string that is seperated by
> the "_" character and changing the text into an integer?
>
> My current code is as follows:
>
> 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
>
> So my goal here is for python to check at the value of "date".  If the
> value of "date[0]" is cyear then I want it to get the current year from the
> computer.  If the value of date[0] is a number then I want to just change it
> into an integer.
>
> Currently the above code does not work unless I change the "if" statement
> to say:
> "if check_year == "c".
>
> Did I do the slice incorrectly?  I thought that when you take the first
> location (0) of a list then it would take the "cyear" in stead of just the
> "c".
>
> All input is appreciated.
>
> Thanks in advance,
>
> Katt
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091106/fb4e1059/attachment.htm>


More information about the Tutor mailing list