On Mon, Nov 28, 2011 at 12:32 PM, Mayo Adams <mayoad...@gmail.com> wrote:

> I am trying to pass a set of tuple strings from a file to a function I
> have defined.  Each tuple is on a separate line, and looks something
> like this:
>  ('note',2048)
> The function has two parameters , and is defined thus: def
> findindex(dval,ticks):
> Apparently, I need to cast the second value as an integer in the body
> of the function in order to work with it as such, but when I attempt
> int(ticks) I get
> ValueError: invalid literal for int() with base 10: '2048)'
>
> Upon searching for elucidation of this on the internet I find nothing
> but esoterica, at least as far as I am concerned.
> Any pointers would make me happy.
>
> --
> Mayo Adams
>
>
>
> mayoad...@gmail.com
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



Your problem is in the error message:

ValueError: invalid literal for int() with base 10: '2048)'

observer, Python tells you this isn't a number: '2048)'

Indeed, this is correct, because you '2048)' is not a number.

What you really want to pass to '2048', which int('2048')

can understand just fine.

So, trim off the parenthesis, or something.

Alternatively, since you aren't actually passing a "tuple" but something
that looks like a python tuple as a string, you could eval it:

a = "('note',2048)"
b = eval(a)

print a, type(a), b, type(b)

>> ('note',2048) <type 'str'> ('note', 2048) <type 'tuple'>

In working with the second, you do normal tuple operations, like b[1] to
access that index

When passing to a function, you do this: findindex(*b)
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to