Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.strip-won';t删除Python3程序中的最后一个引号_Python_Csv_Python 3.x_Input - Fatal编程技术网

.strip-won';t删除Python3程序中的最后一个引号

.strip-won';t删除Python3程序中的最后一个引号,python,csv,python-3.x,input,Python,Csv,Python 3.x,Input,编辑:更新,所以我根据@Alex Thornton的建议运行了这个 这是我的输出: '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,

编辑:更新,所以我根据@Alex Thornton的建议运行了这个

这是我的输出:

'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"
从该文件中读取:

"Type", "MaxAmount"
"SCHOOL","$100.00"
"UTILITIES","$200.00"
"AUTO", "$100.00"
"RENT", "$600.00"
"MEALS", "$300.00"
"RECREATION", "$100.00"
它应该提取预算类型(学校、公用事业等)和最大金额。最大金额应转换为浮动。然而,当我运行程序时,我得到了这个错误

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"
回溯(最近一次呼叫最后一次):
文件“budget.py”,第47行,在
Main()
文件“budget.py”,第43行,主目录
budget=readBudget(“budget.txt”)
readBudget中第22行的文件“budget.py”
条目={'exptype':exptype'maxamnt':float(maxamunt)}
ValueError:float()的文本无效:100.00“
readBudget中的strip函数是否应该删除最后一个引号?

当我尝试此操作时:

>>> attempt = '"$100.00"'
>>> new = attempt.strip('$" \n')
'100.00'
>>> float(new)
100.00
我得到了人们所期望的结果-因此这一定与我们从文件中看不到的内容有关。从您发布的内容来看,不清楚您试图传递给
float()
(因为它看起来非常合理)的字符串是否有细微的错误。请尝试添加调试
print
语句:

print(repr(maxamount))
p_value = float(maxamount)
然后您可以准确地确定传递给
float()
的内容。调用
repr()
将使正常情况下不可见的字符可见。将结果添加到您的问题中,我们将能够进一步评论


编辑:

在这种情况下,请更换:

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

这样就可以了。

添加以下内容:

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

或者更具体地说,删除了错误。

您可以使用正则表达式捕获字符串中的全部或大部分浮点信息

考虑:

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)  
印刷品:

"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

只需修改正则表达式来捕获你认为有效的浮点数据点或无效。

你的程序对我来说很好。这是一个CSV文件:为什么不使用<代码> CSV 模块?它会处理你在一行或两行中所做的几乎所有的处理。对我来说,也可能存在一些问题。txt文件;)repr(列表[1])返回什么?@user1768884:我也使用了Python 2.7,它对我来说运行得很好。
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)  
"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