简体   繁体   English

.strip不会删除Python3程序中的最后一个引号

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

EDIT: UPDATE, so I ran this with @Alex Thornton's suggestion. 编辑:更新,所以我用@Alex Thornton的建议来运行它。

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. 但是在Windows下,我只得到数字列表,加上了引号和\\ r。

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? 现在,我对Windows和Linux处理文本文件的方式不太了解,但这不是由于Windows和Linux处理返回/输入键的方式引起的吗?

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? readBudget中的strip功能是否应删除最后一个引号?

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). 从您发布的内容来看,尚不清楚您尝试传递给float()的字符串是否存在细微的错误(因为它看起来很合理)。 Try adding a debug print statement: 尝试添加调试print语句:

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

Then you can determine exactly what is being passed to float() . 然后,您可以确切确定传递给float() The call to repr() will make even normally invisible characters visible. 调用repr()可以使通常不可见的字符也可见。 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. 那应该工作正常。

Adding in this: 在此添加:

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

Or more specifically, the \\r, removed the error. 更具体地说,\\ r消除了错误。

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. 只需修改正则表达式即可捕获您认为有效的浮点数据点-或无效的数据点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM