0

The system of notation of decimal numbers in the Excel files I am dealing with is (I think) the European system (like French and German). So I have numbers like:

  • '23,4588'
  • '78,056888'

Note that they are in the form of strings. When I try to convert them to float python raises an exception:

>>> float('78,056888')
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: '78,056888'

Here's how I solve the problem:

 try:
     decim = int(excel_value)
 except ValueError:
     remap = {ord(','): '.'}
     decim = float(excel_value.translate(remap))

It seems to work as I want.

So my question is: is there a more appropriate python method for solving these kinds of problems?

1

1 Answer 1

2

You can do this more succinctly with str.replace:

decim = float(excel_value.replace(',', '.'))
Sign up to request clarification or add additional context in comments.

2 Comments

This throws an error. You forgot a closing parenthesis at the end.
You're right, thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.