Python Forum
ValueError: invalid literal for int() with base 10: ''
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ValueError: invalid literal for int() with base 10: ''
#6
Before doing any regex @keames,should first figure out what going on Wink
Like using repr() that i will show later.

@ivinjjunior if i just copy your code from post and run it.
There can be a extra first blank line there,if you copied all in without enter after your post sentence.
with open('numbers.txt') as f:
    for line in f:
        line = line.strip()
        print(int(line))
Output:
2105301 2106301 2108701 2108801 2108901 2109101 2109201 2109501
To see all use repr(),here i just remove strip() then will see \n or any other problem that there can be with input file.
with open('numbers.txt') as f:
    for line in f:
        line = line
        print(repr(line))
'2105301\n'
'2106301\n'
'2108701\n'
'2108801\n'
'2108901\n'
'2109101\n'
'2109201\n'
'2109501'
My guess is that you have blank line in file,then cant try this.
with open('numbers.txt') as f:
    for line in f:
        line = line.strip()
        if line != '':           
            print(int(line))
Reply


Messages In This Thread
RE: ValueError: invalid literal for int() with base 10: '' - by snippsat - Apr-20-2019, 02:40 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  ValueError: invalid literal for int() with base 10: omega_elite 5 5,971 Dec-30-2020, 06:11 AM
Last Post: delonbest
  invalid literal for int() with base 10: '# NRECS: 1096\n' Baloch 8 4,726 May-24-2020, 02:08 AM
Last Post: Larz60+
  invalid literal for int() with base 10: '' mrsenorchuck 5 5,854 Apr-29-2020, 05:48 AM
Last Post: markfilan
  ValueError: invalid literal for int() with base 10: '\n' srisrinu 9 5,964 Apr-13-2020, 01:30 PM
Last Post: ibreeden
  zlib decompress error: invalid code lengths set / invalid block type DreamingInsanity 0 7,014 Mar-29-2020, 12:44 PM
Last Post: DreamingInsanity
  input-ValueError: invalid literal for int() jacklee26 2 2,632 Feb-21-2020, 01:27 PM
Last Post: ndc85430
  ValueError: invalid literal for int() with base 10: '0.5' emmapaw24 2 3,825 Feb-16-2020, 07:24 PM
Last Post: emmapaw24
  ValueError: invalid literal for int() with base 10: '' Jay123 7 7,479 Aug-05-2019, 02:43 PM
Last Post: Jay123
  ValueError: invalid rectstyle object fen1c5 1 5,804 Jun-05-2019, 02:51 PM
Last Post: heiner55
  Problem with "invalid literal for int() with base 10: '' jirkaj4 4 10,552 Jan-23-2018, 06:55 PM
Last Post: jirkaj4

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020