ValueError: invalid literal for int() with base 10

2019-01-01 01:57发布

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

标签: python
12条回答
春风洒进眼中
2楼-- · 2019-01-01 02:18

I got into the same issue when trying to use readlines() inside for loop for same file object... My suspicion is firing readling() inside readline() for same file object caused this error.

Best solution can be use seek(0) to reset file pointer or Handle condition with setting some flag then create new object for same file by checking set condition....

查看更多
大哥的爱人
3楼-- · 2019-01-01 02:19

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.

Something like this should work:

for line in infile:
    next_lines = []
    if line.strip():
        for i in xrange(4):
            try:
                next_lines.append(infile.next())
            except StopIteration:
                break
    # Do your calculation with "4 lines" here
查看更多
唯独是你
4楼-- · 2019-01-01 02:21

Please test this function (split()) on a simple file. I was facing the same issue and found that it was because split() was not written properly (exception handling).

查看更多
墨雨无痕
5楼-- · 2019-01-01 02:23

Reason Is that You are getting a empty string or string as a argument into int check it before is it empty or it contains alpha characters or not if it contains than ignore that part simply.

查看更多
查无此人
6楼-- · 2019-01-01 02:31

The reason you are getting this error is that you are trying to convert a space character to an integer, which is totally impossible and restricted.And that's why you are getting this error.enter image description here

Check your code and correct it, it will work fine

查看更多
流年柔荑漫光年
7楼-- · 2019-01-01 02:34

This could also happen when you have to map space separated integers to a list but you enter the integers line by line using the .input(). Like for example I was solving this problem on HackerRank Bon-Appetit, and the got the following error while compiling enter image description here

So instead of giving input to the program line by line try to map the space separated integers into a list using the map() method.

查看更多
登录 后发表回答