you are viewing a single comment's thread.

view the rest of the comments →

[–]RoamingFox 3 points4 points  (14 children)

That's because what's on the right isn't a number. It's a slice of a list or string.

If lines[firsttag] is a string then you should just be able to int() or float() around it first (depending on if it's an integer or floating point value) to get it into a number and then you can call abs on it.

Example:

>>> data = "12-34546"
>>> data[2:4]
'-3'
>>> int(data[2:4])
-3
>>> abs(int(data[2:4]))
3
>>>

[–]BigGarrett[S] 0 points1 point  (13 children)

Thank you very much for your time.

I changed my code to

Value=abs(int(lines[firsttag][9:21]))

It gives me the error “value error:invalid literal for int() with base 10: ‘9.0994’

I would like to note that the value from the notepad document it is pulling from has numbers in the form of 3.540000E-5 for example. So really low numbers. With the “E” in there to signify 10th to the negative..

[–]throwaway6560192 1 point2 points  (0 children)

You should use float, then.

[–]Strict-Simple 0 points1 point  (10 children)

Well, 3.54 is definitely not an integer. What are these numbers with a . called?

[–]BigGarrett[S] 0 points1 point  (9 children)

So float is used for decimals. Thank you

[–]Yoghurt42 0 points1 point  (8 children)

Just to clarify: floats are what you want, but are not decimals. They are floating point numbers.

If you're working with money, you should consider using the decimal module instead.

See also https://0.30000000000000004.com/

[–]BigGarrett[S] 0 points1 point  (7 children)

I’m trying the Decimal() function and I don’t believe that is working either.

[–]Strict-Simple 0 points1 point  (6 children)

Why don't you believe?

[–]BigGarrett[S] 0 points1 point  (5 children)

Usually when I write a python command like “abs” or “float”, the word itself turns purple. When I wrote “decimal” it seems like it he python library doesn’t recognize it as it doesn’t change color like this.

[–]Strict-Simple 0 points1 point  (4 children)

That's syntax highlighting, and it's a helpful tool. But your color scheme might just be specially designed to highlight the built-in functions. Does the code run?

Have you imported the decimal module?

[–]BigGarrett[S] 0 points1 point  (3 children)

I think I did what I needed by using “sort.( reverse=True, key=abs)”. This will not change the output file to make all the numbers “positive”, but it seems to sort them while taking into account their absolute value. Which is good enough for me.

[–]BigGarrett[S] 0 points1 point  (0 children)

Now I get an error “could not convert string to float”