-3

I have a big function that is importing some lines and most values rows are integers so I can easily make a division like this:

price_unit_calc = float(inv_row[7]) / float(inv_row[6])

but I get an error:

ValueError: invalid literal for float(): 1,000.000

this is because inv_row[7] sometimes is a "1,000.000" and not 1.000.00

Question is how can I remove those " if they appear in a row?

UPDATE: if I do

 price_unit_calc = float(inv_row[7].replace(',','.')) / float(inv_row[6].replace(',','.'))

i still get

File "/home//workspace/odoo-9.0/addons/config/wizard/import_wizard.py", line 39, in do_import
    price_unit_calc = float(inv_row[7].replace(',','.')) / float(inv_row[6].replace(',','.'))
ValueError: invalid literal for float(): 1.000.000

UPDATE2:

price_unit_calc = float(inv_row[7].replace(',','')) / float(inv_row[6].replace(',',''))

and error

File "/home/antonp/workspace/odoo-9.0/openerp/osv/fields.py", line 362, in _symbol_set_float
    result = __builtin__.float(x or 0.0)
ValueError: invalid literal for float(): 1,000.000
Chaban33
  • 1,362
  • 11
  • 38
  • 5
    No. the `,` is the error here. So, remove it before converting to float. `inv_row[7].replace(",", "")` – scharette Aug 07 '18 at 14:56
  • remove the comma with `str.replace` and it should work – Olivier Melançon Aug 07 '18 at 14:58
  • `1.000.000` is also an invalid literal. If you really want to have a separator for the various places, you can use underscores: `1_000.000` is the same as `1000.0` – Patrick Haugh Aug 07 '18 at 15:01
  • You've replaced `','` with `'.'`. That's still invalid. You should have replaced `','` with an empty string `''`. – khelwood Aug 07 '18 at 15:02
  • Now you're getting a similar error in a different place in your code. – khelwood Aug 07 '18 at 15:05
  • Possible duplicate of [Python - convert string to float (the conventional method, float(), is not working in this case)](https://stackoverflow.com/questions/16201286/python-convert-string-to-float-the-conventional-method-float-is-not-worki) – khelwood Aug 07 '18 at 15:10

2 Answers2

2

As I said in the comment section the , is the problem. So, you need to remove it since it is only relevant for formatting (it doesn't change the actual value)

inv_row[7].replace(",", "")

Also, if your inv_row is a list containing only floats values like 1,000.000, what you should do instead is iterate over the list and apply the above logic instead of finding everywhere in your code where it could possibly result in an error,

for index,item in enumerate(inv_row):
     inv_row[index] = item.replace(",", "")

Now, for what you tried,

 inv_row[7].replace(",", ".")

This will result in adding multiple . to your string representation will which will also lead to the error.

scharette
  • 9,437
  • 8
  • 33
  • 67
  • well now its File "/home/antonp/workspace/odoo-9.0/openerp/osv/fields.py", line 362, in _symbol_set_float result = __builtin__.float(x or 0.0) ValueError: invalid literal for float(): 1,000.000 – Chaban33 Aug 07 '18 at 15:03
  • 1
    @Chaban33 That's the same problem. You need to remove the comma from your string. – khelwood Aug 07 '18 at 15:03
  • @Chaban33 No, that's the error at another place in your code where you have the same problem. – khelwood Aug 07 '18 at 15:06
  • `ValueError: invalid literal for float(): 1,000.000` there is clearly still a comma in one of your value. – scharette Aug 07 '18 at 15:06
2

You have to remove the commas in the numbers by replacing them with empty strings:

price_unit_calc = float(inv_row[7].replace(',','')) / float(inv_row[6].replace(',',''))
blhsing
  • 91,368
  • 6
  • 71
  • 106