Re: Convert Str to Float after reading from File

Jeff Emanuel <[email protected]>
Newsgroups gmane.comp.lang.jython.user
Message-ID <[email protected]>
I suggest, this to see if there is whitespace in your Amount string:

   Amount=x[0]
   try:
     x=float(Amount)
   except ValueError:
     print repr(Amount)

You might need strip() after split()

I can't tell what you are trying to accomplish by this, but it's not doing you any good getting to a parsable string.

    ltrg = ';'.join(valueList[12:13])



On 8/8/2015 12:33 PM, Peter O'Reilly wrote:
> I am reading from a file and convert a columns from a list value to a string.
>
> Then I try and convert from a str to a float, but get this error.
> x=float(Amount)
> ValueError: invalid literal for __float__:
>
> If I copy the value from the file and assign it to the str variable(Amount), the float function works as expected.
>
> It seems like it might be some type of encoding issue, anyone else have a  similar problem?  This is the code.
>
> while (lsrc):
>         valueList = lsrc.split(';')
>         ltrg = ';'.join(valueList[12:13])
>         x=ltrg.strip().split('\r\n')
>         Amount=x[0]
>         x=float(Amount)
>
> Thanks,
> PR
>
>
> ------------------------------------------------------------------------------
>
>
> _______________________________________________
> Jython-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/jython-users

------------------------------------------------------------------------------

_______________________________________________
Jython-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jython-users
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.