找到你要的答案

Q:Slice a number in a file

Q:在文件中插入一个数字

I have a number 1215 in the file called Lista.txt.

I want to take the number 12 contained in the line, add 1, and print the result which should be 13.

But I am getting this error:

Could not convert string to float error.

palabra=open('Lista.txt')
print palabra
for i in palabra:
    a = float(i[:2])
    b=a+1
    print b

What is causing this?

我有一个1215号文件中称lista.txt。

我想把12号线中包含的数字,加上1,打印结果应该是13。

但我得到这个错误:

无法将字符串转换为浮点错误。

palabra=open('Lista.txt')
print palabra
for i in palabra:
    a = float(i[:2])
    b=a+1
    print b

是什么原因造成的?

answer1: 回答1:

Well, I was trying to run it and I haven't saved the file, so it caused conflicts, also I looked for spaces in the file, saved, closed and run again the script.

好吧,我试图运行它,我没有保存文件,所以它造成冲突,我也在文件中寻找空间,保存,关闭并再次运行脚本。

python  string  file  slice