Invalid Operation with Decimal

zach_hutz

I'm using beautiful soup to extract information from a website and to get the price of an item. I used the code below to create a variable names prices to store the information. Then I created a loop to iterate through all of the items and now I'm trying to compare it to another variable named price_thres to determine if its less than or equal to the amount. Running this code prints a few of the correct values but it also prints

    price_in_dec = Decimal(i.text)
decimal.InvalidOperation: [<class 'decimal.ConversionSyntax'>]
prices = soup.find_all("span", itemprop="price")

for i in prices:
    price_in_dec = Decimal(i.text)
    if price_in_dec <= price_thres:
        print(price_in_dec)
Bharel

You seem to have commas (,) in some of your prices.

Try this:

import locale

# Set to US locale
locale.setlocale(locale.LC_ALL, 'USA')

prices = soup.find_all("span", itemprop="price")

for i in prices:
    price_in_dec = Decimal(locale.atof(i.text))
    if price_in_dec <= price_thres:
        print(price_in_dec)

By using the correct locale, you can parse the prices according to the way you write them in the US.

Another option would be to simply remove commas using i.text.replace(",", "").

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Invalid hex to decimal value

From Dev

Invalid operation of opcode and operands

From Dev

Invalid argument to operation ++/--

From Dev

Invalid Floating Point Operation

From Dev

E: Invalid operation instal

From Dev

E: Invalid operation instal

From Dev

OpenGL Invalid Operation

From Dev

Invalid argument to operation ++/--

From Dev

CPython and PyPy Decimal operation performance

From Dev

interger and decimal operation speed in JavaScript

From Dev

As operation 'decimal' * 'decimal' result in a loss of precision but is authorized, why is the operation 'decimal' * 'double' forbidden?

From Dev

glGenBuffers - GL_INVALID_OPERATION

From Dev

LWJGL glDrawArrays throws Invalid operation

From Dev

Deserialize XML Invalid Operation Exception

From Dev

GL ERROR: 1282 - Invalid operation

From Dev

Modulo operation on a python negative decimal.Decimal and a positive int

From Dev

How to get decimal by long divide operation

From Dev

Invalid cast from Int32 to Decimal

From Dev

Array operations sometimes throws 'Operation is invalid after previous operation'

From Dev

glVertexAttribPointer GL_INVALID_OPERATION invalid vao vbo pointer usage

From Dev

Webgl readPixels() invalid operation: invalid format/type combination

From Dev

GL_INVALID_OPERATION on Tesselation Shader

From Dev

Invalid floating point operation calling Trunc()

From Dev

OpenGL: INVALID_OPERATION following glEnableVertexAttribArray

From Dev

JDBC to hive connection fails on invalid operation isValid()

From Dev

Trying to install OpenFL - haxelib returns invalid operation

From Dev

Invalid operation for read only resultset: updateString

From Dev

glNamedBufferData fires GL_INVALID_OPERATION

From Dev

glVertexAttribPointer raising impossible GL_INVALID_OPERATION?

Related Related

  1. 1

    Invalid hex to decimal value

  2. 2

    Invalid operation of opcode and operands

  3. 3

    Invalid argument to operation ++/--

  4. 4

    Invalid Floating Point Operation

  5. 5

    E: Invalid operation instal

  6. 6

    E: Invalid operation instal

  7. 7

    OpenGL Invalid Operation

  8. 8

    Invalid argument to operation ++/--

  9. 9

    CPython and PyPy Decimal operation performance

  10. 10

    interger and decimal operation speed in JavaScript

  11. 11

    As operation 'decimal' * 'decimal' result in a loss of precision but is authorized, why is the operation 'decimal' * 'double' forbidden?

  12. 12

    glGenBuffers - GL_INVALID_OPERATION

  13. 13

    LWJGL glDrawArrays throws Invalid operation

  14. 14

    Deserialize XML Invalid Operation Exception

  15. 15

    GL ERROR: 1282 - Invalid operation

  16. 16

    Modulo operation on a python negative decimal.Decimal and a positive int

  17. 17

    How to get decimal by long divide operation

  18. 18

    Invalid cast from Int32 to Decimal

  19. 19

    Array operations sometimes throws 'Operation is invalid after previous operation'

  20. 20

    glVertexAttribPointer GL_INVALID_OPERATION invalid vao vbo pointer usage

  21. 21

    Webgl readPixels() invalid operation: invalid format/type combination

  22. 22

    GL_INVALID_OPERATION on Tesselation Shader

  23. 23

    Invalid floating point operation calling Trunc()

  24. 24

    OpenGL: INVALID_OPERATION following glEnableVertexAttribArray

  25. 25

    JDBC to hive connection fails on invalid operation isValid()

  26. 26

    Trying to install OpenFL - haxelib returns invalid operation

  27. 27

    Invalid operation for read only resultset: updateString

  28. 28

    glNamedBufferData fires GL_INVALID_OPERATION

  29. 29

    glVertexAttribPointer raising impossible GL_INVALID_OPERATION?

HotTag

Archive