Getting int error, but I do not know why. My file has no extra spaces

Viewed 92

I have a problem, but I can't figure out where it is going wrong.

What I am getting from this is that there is a empty thing or a space but there definitively isn't any in my text file. The goal of my code is to add money to a text file separated by a comma and turn that into an integer list.

Bonus: If you know anything about Pycharm how do you get rid of the '>?' when typing in an input.

My error:

File "C:/Users/hayde/PycharmProjects/Finance/Money.py", line 32, in <listcomp>
    int_tot = [int(i) for i in str_tot]
ValueError: invalid literal for int() with base 10: ''

My code:

def add_money():
    money = str(input("Amount: "))   
    #if '' in money: #if i run this it loops forever
        #return add_money()
    with open(r"C:\Users\hayde\OneDrive\Desktop\account.txt", 'a') as account:  
        account.write(',' + money)                    
    with open(r"C:\Users\hayde\OneDrive\Desktop\account.txt", 'r') as account:  
        if account.read(1) == ',':                                 
            print("Please clear account file, then have 0 be the only value in file")
            return                  
        for cash in account:
            str_tot = cash.split(',')  
    int_tot = [int(i) for i in str_tot]
    return int_tot
4 Answers

One reason for getting that error is when you call int() on a string that is empty:

int_tot = [int(i) for i in str_tot]

You can use a filter to only convert and keep non-empty strings:

int_tot = [int(i) for i in str_tot if i]

if i in this case, means if i != ''.

When you split the line by a comma, if the first character in that line is a comma, the first value in the list is ''. So, that is why the error arises.

To fix this, don't add a comma to a new line or don't add a comma as the first character of a line.
Or you can simply remove the '' from the list by .remove(0).

The problem seems to be that on str_tot one or more elements can not be converted to int that is why you are getting a ValueError. Please check the file to verify that you don't have strings or blank spaces, you could also add an exception handling as follow:

str_tot = [1, 2, 3, '3rjr', 6, 7]
int_tot = []

for i in str_tot:
    try:
        int_tot.append(int(i))
    except ValueError:
        print('Error, value: ', i)
print(int_tot)

Output:

Error, value:  3rjr
[1, 2, 3, 6, 7]

There are a few things I ran into running your code. It was really hard for me to actual create a file without a trailing newline, which would be considered a space.

I was able to create such a file with echo -n '0' > account.txt, this will delete your text file and replace it with just a 0.

Second, is that once you call account.read(1) you actually move the file pointer forward so that account is now offset by 1 character. Then when you try to iterate the file pointer is off by one, always starting at a comma because 0 is the start of the file. So if you call account.seek(0) before splitting it should work.

def add_money():
    money = str(input("Amount: "))   
    #if '' in money: #if i run this it loops forever
        #return add_money()
    with open(r"C:\Users\hayde\OneDrive\Desktop\account.txt", 'a') as account:  
        account.write(',' + money)                    
    with open(r"C:\Users\hayde\OneDrive\Desktop\account.txt", 'r') as account:
        if account.read(1) == ',':                                 
            print("Please clear account file, then have 0 be the only value in file")
            return
        # Return to the beginning of the file after reading 1 character.
        account.seek(0)
        
        for cash in account:
            str_tot = cash.split(',')  
    int_tot = [int(i) for i in str_tot]
    return int_tot
Related