Skip to main content

Python · 5 common causes

ValueError: invalid literal for int()

Updated

int() was handed a string that is not a whole number written in base 10.

The exact message

Traceback (most recent call last):
  File "/app/parse.py", line 7, in <module>
    quantity = int(row['qty'])
               ^^^^^^^^^^^^^^^
ValueError: invalid literal for int() with base 10: '12.5'

Paths, line numbers and stack frames differ on every machine, and the wording is tightened between versions. The part that identifies the problem is ValueError: invalid literal for int().

How to fix it

Cause 1 of 5

The string holds a decimal number.

int() will not parse '12.5' — it refuses rather than guessing what to do with the fraction. Convert through float first: int(float(value)) truncates toward zero, round(float(value)) rounds to nearest.

Cause 2 of 5

The field is empty or missing.

int() tolerates surrounding whitespace but not an empty string. Check before converting: value = value.strip() then int(value) if value else 0 — and decide deliberately whether a blank should become zero or be rejected.

Cause 3 of 5

The value carries formatting — separators, currency symbols, units.

'1,234' and '$99' are not integer literals. Strip the decoration explicitly, for example int(value.replace(',', '').lstrip('$')), so anything unexpected still fails loudly instead of parsing wrong.

Cause 4 of 5

The offending value in the message is your best clue — if it reads 'qty' you are parsing the header. Use csv.DictReader, which consumes the header row for you, or skip the first line explicitly.

Cause 5 of 5

The number is in another base.

Pass the base rather than assuming 10: int('ff', 16) for hex, int('1010', 2) for binary, int('0x1f', 0) to honour a literal prefix.

Stopping it coming back

Parse and validate at the point data enters the program — a Pydantic model or an explicit converter per column — so a bad value is reported with its row number instead of crashing three functions later.

ValueError: invalid literal for int() — common questions

How do I check whether a string is a valid int first?
Wrap the conversion in try/except ValueError. str.isdigit() looks tempting but returns False for '-5' and True for some non-ASCII digit characters, so it is not a reliable gate.
Why does int() accept ' 42\n' but not ''?
Leading and trailing whitespace is explicitly ignored by the parser, so values read from a file with a trailing newline still work. An empty string has no digits at all, so there is nothing to parse.
Should I use int(float(x)) for money?
No. Binary floats cannot represent decimal amounts exactly, so rounding drifts. Use decimal.Decimal(value) for currency, or store the amount in the smallest unit as an integer.

Test the fix before you paste it back

Reproducing the failure in a scratch file is usually faster than editing the real one. AltF Code is a free in-browser editor with real syntax highlighting for HTML, CSS, JavaScript, TypeScript, Python, SQL and JSON, and it runs HTML, CSS and JavaScript live in a preview panel. No install and no sign-up.

Open the editor