Python Forum
ValueError: invalid literal for int() with base 10: '' - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: ValueError: invalid literal for int() with base 10: '' (/thread-20300.html)



ValueError: invalid literal for int() with base 10: '' - Jay123 - Aug-04-2019

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.


RE: ValueError: invalid literal for int() with base 10: '' - ndc85430 - Aug-04-2019

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)?


RE: ValueError: invalid literal for int() with base 10: '' - Jay123 - Aug-04-2019

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.


RE: ValueError: invalid literal for int() with base 10: '' - ndc85430 - Aug-04-2019

You could add an if to your list comprehension to do the check.


RE: ValueError: invalid literal for int() with base 10: '' - Jay123 - Aug-04-2019

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


RE: ValueError: invalid literal for int() with base 10: '' - ndc85430 - Aug-04-2019

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.


RE: ValueError: invalid literal for int() with base 10: '' - perfringo - Aug-05-2019

(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]



RE: ValueError: invalid literal for int() with base 10: '' - Jay123 - Aug-05-2019

Thanks for the help @ perfringo that works perfectly.

Also thanks to @ndc85430 for the pointers.