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: ''
#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


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

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