Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
427 views
in Technique[技术] by (71.8m points)

python - ValueError:以10为底的int()的无效文字:''(ValueError: invalid literal for int() with base 10: '')

I am creating a program that reads a file and if the first line of the file is not blank, it reads the next four lines.

(我正在创建一个读取文件的程序,如果文件的第一行不是空白,它将读取接下来的四行。)

Calculations are performed on those lines and then the next line is read.

(在这些行上执行计算,然后读取下一行。)

If that line is not empty it continues.

(如果该行不为空,则继续。)

However, I am getting this error:

(但是,我收到此错误:)

ValueError: invalid literal for int() with base 10: ''.

It is reading the first line but can't convert it to an integer.

(它正在读取第一行,但无法将其转换为整数。)

What can I do to fix this problem?

(我该怎么做才能解决此问题?)

The code:

(编码:)

file_to_read = raw_input("Enter file name of tests (empty string to end program):")
try:
    infile = open(file_to_read, 'r')
    while file_to_read != " ":
        file_to_write = raw_input("Enter output file name (.csv will be appended to it):")
        file_to_write = file_to_write + ".csv"
        outfile = open(file_to_write, "w")
        readings = (infile.readline())
        print readings
        while readings != 0:
            global count
            readings = int(readings)
            minimum = (infile.readline())
            maximum = (infile.readline())
  ask by Sarah Cox translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just for the record:

(仅作记录:)

>>> int('55063.000000')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '55063.000000'

Got me here...

(我在这里...)

>>> float('55063.000000')
55063.0

Has to be used!

(必须使用!)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...