user1768884
user1768884

Reputation: 1047

.strip won't remove last quotation mark in Python3 Program

EDIT: UPDATE, so I ran this with @Alex Thornton's suggestion.

This is my output:

'100.00"\r'
Traceback (most recent call last):
  File "budget.py", line 48, in <module>
    Main()
  File "budget.py", line 44, in Main
    budget = readBudget("budget.txt")
  File "budget.py", line 21, in readBudget
    p_value = float(maxamount)
ValueError: invalid literal for float(): 100.00"

Under Windows though, I just get the list of numbers, with the qutations and the \r's stripped off.

Now, I don't know too much about the way Windows and Linux handle text files, but isn't it due to the way Windows and Linux handle the return/enter key?

So I have this code:

def readBudget(budgetFile):
    # Read the file into list lines
    f = open(budgetFile)
    lines = f.readlines()
    f.close()

    budget = []

    # Parse the lines
    for i in range(len(lines)):
        list = lines[i].split(",")

        exptype = list[0].strip('" \n')
        if exptype == "Type":
            continue

        maxamount = list[1].strip('$" \n')

        entry = {'exptype':exptype, 'maxamnt':float(maxamount)}

        budget.append(entry)

    #print(budget)

    return budget

def printBudget(budget):
    print()
    print("================= BUDGET ==================")
    print("Type".ljust(12), "Max Amount".ljust(12))

    total = 0
    for b in budget:
        print(b['exptype'].ljust(12), str("$%0.2f" %b['maxamnt']).ljust(50))
        total = total + b['maxamnt']

    print("Total: ", "$%0.2f" % total)

def Main():
    budget = readBudget("budget.txt")
    printBudget(budget)

if __name__ == '__main__':    
    Main()

Which reads from this file:

"Type", "MaxAmount"
"SCHOOL","$100.00"
"UTILITIES","$200.00"
"AUTO", "$100.00"
"RENT", "$600.00"
"MEALS", "$300.00"
"RECREATION", "$100.00"

It is supposed to extract the budget type (school, utilities, etc) and the max amount. The max amount is supposed to be converted to a float. However, when I run the program, I get this error.

Traceback (most recent call last):
  File "budget.py", line 47, in <module>
    Main()
  File "budget.py", line 43, in Main
    budget = readBudget("budget.txt")
  File "budget.py", line 22, in readBudget
    entry = {'exptype':exptype, 'maxamnt':float(maxamount)}
ValueError: invalid literal for float(): 100.00"

Shouldn't the strip function in readBudget remove the last quotation mark?

Upvotes: 0

Views: 151

Answers (3)

dawg
dawg

Reputation: 103824

You can use a regex to capture either all or most of the floating point info in a string.

Consider:

import re

valid='''\
123.45"
123.
123"
.123
123e-16
-123e16
123e45
+123.45'''

invalid='''\
12"34
12f45
e123'''

pat=r'(?:^|\s)([-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)'

for e in [valid, invalid]:
    print
    for line in e.splitlines():
        m=re.search(pat, line)
        if m:
            print '"{}" -> {} -> {}'.format(line, m.group(1), float(m.group(1)))
        else:
            print '"{}" not valid'.format(line)  

Prints:

"123.45"" -> 123.45 -> 123.45
"123." -> 123 -> 123.0
"123"" -> 123 -> 123.0
".123" -> .123 -> 0.123
"123e-16" -> 123e-16 -> 1.23e-14
"-123e16" -> -123e16 -> -1.23e+18
"123e45" -> 123e45 -> 1.23e+47
"+123.45" -> +123.45 -> 123.45

"12"34" -> 12 -> 12.0
"12f45" -> 12 -> 12.0
"e123" not valid

Just modify the regex to capture what you consider a valid floating point data point -- or invalid.

Upvotes: 0

anon582847382
anon582847382

Reputation: 20361

When I tried this:

>>> attempt = '"$100.00"'
>>> new = attempt.strip('$" \n')
'100.00'
>>> float(new)
100.00

I got exactly what one would expect- so it must be something to do with what we cannot see from the file. From what you've posted, it's not clear whether there is something subtly wrong with the string you're trying to pass to float() (because it looks perfectly reasonable). Try adding a debug print statement:

print(repr(maxamount))
p_value = float(maxamount)

Then you can determine exactly what is being passed to float(). The call to repr() will make even normally invisible characters visible. Add the result to your question and we will be able to comment further.


EDIT:

In which case, replace:

maxamount = list[1].strip('$" \n')

With:

maxamount = list[1].strip('$" \n\r')

That should then work fine.

Upvotes: 1

user1768884
user1768884

Reputation: 1047

Adding in this:

maxamount = list[1].strip('$" \n\r')

Or more specifically, the \r, removed the error.

Upvotes: 0

Related Questions