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: ''
#1
Hello everyone, here again..

i am getting a problem with a part of my coding
numero = int(input('informe o pedido\n:'))
pedido = open(f'Pedidos_test\{numero}.txt', 'r')
teste = []
testa = []
cont = 0
indic_pedido = 0
for linha in pedido:
    linha = linha.rstrip()
    novo = int(linha)
    print(novo)
its giving me the error:
Traceback (most recent call last):
File "C:/Users/ivinj/PycharmProjects/mundo3/estresseeeeee.py", line 9, in <module>
novo = int(linha)
ValueError: invalid literal for int() with base 10: ''




I really don't know what it might be..

please help mee Sad
Reply
#2
Quote:I really don't know what it might be
Print linha and see for yourself. We have no way of knowing.
Reply
#3
i'm using the document containing the following:

2105301
2106301
2108701
2108801
2108901
2109101
2109201
2109501
Reply
#4
I suspect that it might be file corruption. There may be leading or trailing invisible characters, or even invisible characters inside your integer literals which python isn't recognizing as whitespace. The ascii code for "\n" is 10. The ascii codes for 0-9 are 48 through 58 inclusive. If you run the following script on your file, it will detect anything that isn't in this character set:
import re
# Open the file you want to scan for invalid characters.
numero = int(input('informe o pedido\n:'))
# We use regular expression matching to detect the bad characters.
validChar = re.compile(r"\A\d|\n\Z")
with open(f'Pedidos_test\{numero}.txt', 'r') as f:
    for lineNum, lineVal in enumerate(f.readlines()):
        for charNum, currentChar in enumerate(lineVal):
            if not validChar.search(currentChar):
                print("Invalid character detected on line {}, at char {}".format(lineNum + 1, charNum + 1))
                print("Invalid character code: {}".format(ord(currentChar)))
If I'm right, and you have a bad file, the unwanted characters will be invisible, so we just print out their code points to see what they are. To remove them, we just run the script below:
# Open the file you want to scan for invalid characters.
numero = int(input('informe o pedido\n:'))
# We use regular expression matching to detect the bad characters.
validChar = re.compile(r"\A\d|\n\Z")
cleanStr = ""
with open(f'Pedidos_test\{numero}.txt', 'r') as f:
    for char in f.read():
        if validChar.search(char): cleanStr += char

with open(f'Pedidos_test\{numero}.txt', 'w') as f:
    f.write(cleanStr)
Reply
#5
I believe the problem may be on line 2 where you open the file. You may need to put at the end "rb" rather than "r". "rb" means read bytes. If it reads it in a different way it may class the info differently causing the int to not work.

Then again, it may not matter whether you use "r" or "rb"
Reply
#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
#7
It's also worth mentioning that if there is other invalid data in the file, regex may be his only option. It wouldn't be too hard to add to your code, @snippsat.
It's literally just the import and 1 additional line:
import re
validNumPattern = re.compile(r"\A\d+\Z")
with open('numbers.txt') as f:
    for line in f:
        line = line.strip()
        if validNumPattern.search(line):           
            print(int(line))
It can't hurt to be thorough...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ValueError: invalid literal for int() with base 10: omega_elite 5 5,677 Dec-30-2020, 06:11 AM
Last Post: delonbest
  invalid literal for int() with base 10: '# NRECS: 1096\n' Baloch 8 4,457 May-24-2020, 02:08 AM
Last Post: Larz60+
  invalid literal for int() with base 10: '' mrsenorchuck 5 5,326 Apr-29-2020, 05:48 AM
Last Post: markfilan
  ValueError: invalid literal for int() with base 10: '\n' srisrinu 9 5,579 Apr-13-2020, 01:30 PM
Last Post: ibreeden
  zlib decompress error: invalid code lengths set / invalid block type DreamingInsanity 0 6,740 Mar-29-2020, 12:44 PM
Last Post: DreamingInsanity
  input-ValueError: invalid literal for int() jacklee26 2 2,509 Feb-21-2020, 01:27 PM
Last Post: ndc85430
  ValueError: invalid literal for int() with base 10: '0.5' emmapaw24 2 3,671 Feb-16-2020, 07:24 PM
Last Post: emmapaw24
  ValueError: invalid literal for int() with base 10: '' Jay123 7 7,217 Aug-05-2019, 02:43 PM
Last Post: Jay123
  ValueError: invalid rectstyle object fen1c5 1 5,618 Jun-05-2019, 02:51 PM
Last Post: heiner55
  Problem with "invalid literal for int() with base 10: '' jirkaj4 4 10,384 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