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

View: New views
6 Messages — Rating Filter:   Alert me  

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

by Katt-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

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

by Wayne Werner :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, Nov 5, 2009 at 8:44 PM, Katt <the_only_katala@...> wrote:
<snip>
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".


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

_______________________________________________
Tutor maillist  -  Tutor@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

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

by Shashwat Anand :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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


_______________________________________________
Tutor maillist  -  Tutor@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

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

by Shashwat Anand :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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")

l0nwlf-Arena:l0nwlf$ python katt.py
2009
1984

http://codepad.org/RBjKmNcA


Hope this helps !

_______________________________________________
Tutor maillist  -  Tutor@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

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

by Alan Gauld :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


"Katt" <the_only_katala@...> wrote

> 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
>
> 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".

When debugging this kind of thing insert some print statements
to check what date2 and check_year really look like. Or try using
the >>> prompt to experiment until you are happy with the behaviour
of the function with differernt sample data.

HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 


_______________________________________________
Tutor maillist  -  Tutor@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

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

by Dave Angel :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Alan Gauld wrote:

> <div class="moz-text-flowed" style="font-family: -moz-fixed">
> "Katt" <the_only_katala@...> wrote
>
>> 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
>>
>> 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".
>
> When debugging this kind of thing insert some print statements
> to check what date2 and check_year really look like. Or try using
> the >>> prompt to experiment until you are happy with the behaviour
> of the function with differernt sample data.
>
> HTH,
>
>
Several things I'd add.

1) You forgot to include the line
      from time import localtime

2) You don't specify the python version or OS environment you're running
on (though I don't think it matters here)

3) The example "works" as it is, meaning the localtime() function is
called, and year is set to 2009 (when I run it today with CPython
2.6.2)  So I'm guessing you retyped the example into your message.  
Always use copy/paste, and if practical, show the printed output results
you got, or the error/traceback if it got an error.

4) You use the word "slice" in your query, but there are no slices in
the program.  The line that binds check_year has a "subscript"
operation, which is similar to a slice, but not the same.  The syntax is
different, in that there's no colon in the square brackets.  And of
course the meaning is different.



If I had to guess, I'd say that somewhere in your real code, you have a
second subscript going on.  If you use a subscript on a list of strings,
you get a string.  If you use a subscript on that, you get another
string, consisting of a single character.


So if
date2 == ["cyear", "11", "05"]
date2[0] == "cyear"
date2[0][0] == "c"


DaveA
_______________________________________________
Tutor maillist  -  Tutor@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor