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

Categories

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

artificial intelligence - Value Error: Invalid Literal for int() with base 10: 'import' - Python

When I'm running this code:

def main():

    maze = []

    maze_1d_arr = open(sys.argv[0], 'r')

    maze_read = maze_1d_arr.readline()

    maze_split = maze_read.split(" ")

    size_X = int(maze_split[0])
    size_Y = int(maze_split[1])

    maze_grid = int(maze_split[2:])

    maze = np.array(maze_grid).reshape(size_X, size_Y)

    start = np.where(maze_split == 2)
    end = np.where(maze_split == 3)

    path = astar(maze, start, end)
    print(path)

I am getting this error;

    size_X = int(maze_split[0])
ValueError: invalid literal for int() with base 10: 'import'

Is there any way this can be fixed? It is coming from a text file which has this one line:

6 4 0 0 1 0 0 0 2 0 1 0 1 1 0 0 1 0 0 0 0 0 0 0 3 0

As a bit of background on the file, the first number is supposed to represent the x axis, the second number is supposed to represent the y axis and the third number onwards is the grid.

Many thanks :)

EDIT: I have changed the code to this:

def main():

    maze = []

    maze_1d_arr = open(sys.argv[1], 'r')

    maze_read = maze_1d_arr.readline()

    maze_split = maze_read.split(" ")

    size_X = [int (X) for X in maze_split[0]]
    size_Y = [int (Y) for Y in maze_split[1]]

    maze_grid = [int (x) for x in maze_split[2:]]

    maze = np.array(maze_grid).reshape(size_X, size_Y)

    start = np.where(maze_split == 2)
    end = np.where(maze_split == 3)

    path = astar(maze, start, end)
    print(path)

But I'm now getting this error,

size_Y = [int (Y) for Y in maze_split[1]]
IndexError: list index out of range

I am still using the same file as before for the code, any ideas on how to fix this?


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

1 Answer

0 votes
by (71.8m points)

This error comes from the fact that you do not use the correct argument from sys.argv: the first element (index 0) is the name of your program, not the first argument passed to the program.

Therefore, the line open(sys.argv[0], 'r') opens the source code of your program (instead of your expected data file). As a result, the first word that you try to parse is the Python keyword import, which is not a valid integer.

The solution is to replace sys.argv[0] by sys.argv[1], which should be the correct argument. See https://docs.python.org/3/library/sys.html#sys.argv for more details on sys.argv.

A little reminder that may help: traditionally, Unix programs use their name when printing the help message; to do so, they need to know the name with which they were invoked, which is the first argument on the command line, i.e., sys.argv[0]. As long as you remember this, you should remember that sys.argv[1] is the first argument of your program.

Good luck on your journey with Python!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...