ValueError:int() 的无效文字以 10 为基数:'' [英] ValueError: invalid literal for int() with base 10: ''

查看:66
本文介绍了ValueError:int() 的无效文字以 10 为基数:''的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个读取文件的程序,如果文件的第一行不为空,它会读取接下来的四行.对这些行进行计算,然后读取下一行.如果该行不为空,则继续.但是,我收到此错误:

ValueError: 以 10 为基数的 int() 的文字无效:''.

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

我该怎么做才能解决这个问题?

代码:

file_to_read = raw_input("输入测试文件名(空字符串结束程序):")尝试:infile = open(file_to_read, 'r')而 file_to_read != " ":file_to_write = raw_input("输入输出文件名(.csv 将附加到它):")file_to_write = file_to_write + ".csv"outfile = 打开(file_to_write,w")读数 = (infile.readline())打印读数读数时!= 0:全局计数读数 = int(读数)最小值 = (infile.readline())最大值 = (infile.readline())

解决方案

仅供参考:

<预><代码>>>>int('55063.000000')回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中.ValueError:int() 的无效文字,基数为 10:'55063.000000'

让我在这里...

<预><代码>>>>int(float('55063.000000'))55063

必须使用!

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())

解决方案

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...

>>> int(float('55063.000000'))
55063

Has to be used!

这篇关于ValueError:int() 的无效文字以 10 为基数:''的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆