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
Hi I have the following text file that I have uploaded here:

This data sits in a larger data set in a text file but I have just extracted the set of data I am interested in and uploaded it into a separate text file.

Data

Instead of reading these as strings I would like to read them as ints into a list. I have tried the following code but get an error. Any help would be much appreciated. Thanks!

 import tkinter as tk
 from tkinter import filedialog
 file_path = filedialog.askopenfilename()
 print(file_path)
 data =  []
 data2 = []
 data3 = []
 flag= False
 with open(file_path,'r') as f:
     for line in f:
         if line.strip().startswith('*NSET, NSET=Nodes_Pushed_Back_IB'):
             flag= True
         elif line.strip().endswith('*NSET, NSET=Nodes_Pushed_Back_OB'):
             flag= False    #loop stops when condition is false i.e if false do nothing
         elif flag:          # as long as flag is true append
             data.append([int(x) for x in line.strip().split(',')]) 
Get the following error:

Error:
ValueError: invalid literal for int() with base 10: ''
I can't figure out how to get rid of the '' that is created when I strip() the lines.
Reply
#2
The problem is that you end up with an empty string, which obviously doesn't represent an integer value. So what is your program supposed to do in the case of an empty string after doing the split (presumably because the line was empty)?
Reply
#3
Hi cdc85430 thanks for the reply. Yes I was hoping to remove the empty string. So I am just left with a list of numbers.
Reply
#4
You could add an if to your list comprehension to do the check.
Reply
#5
Ok could you give me an idea of what that would look like. I presume you are talking about

if line == ""
do not append or something. Thanks
Reply
#6
In a list comprehension, the if is used to only select items where the condition is true (i.e. it's the behaviour same as filter). See the examples here. I also suggest you try playing with list comprehensions including an if to get a feel for how they work, if it isn't immediately obvious.
Reply
#7
(Aug-04-2019, 07:10 AM)Jay123 Wrote:
if line == ""

Empty string is false, so you can just write if line:

[int(x) for x in line.strip().split(',') if x]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#8
Thanks for the help @ perfringo that works perfectly.

Also thanks to @ndc85430 for the pointers.
Reply


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