Why is VS Code unable to convert this List into a Int?
Question:
def load_matrix():
matrix = sys.stdin.read()
matrix = matrix.strip().split('n')
matrix = [row.split(' ') for row in matrix]
for m in matrix:
matrix=[int(l) for l in m]
print("n Data Loaded Sucessfully")
print("n")
var=pd.DataFrame(matrix)
return var
Look at this piece of code. I am trying to take a matrix directly as the input for the program, but the read function takes the input as a list of strings. To convert it into a int object for performing math functions, i have used the code above. running fine with online compilers but VS Code keeps throwing an error saying:
ValueError: invalid literal for int() with base 10: ‘4x1a’
(when given the input as:-
This )
From what I have researched, this happens when the value passed cannot be converted into an integer.
When I run a similar code in Jupyter notebook it runs smoothly.
Your Help will be appreciated.
Answers:
As pointed in the comments, x1a
is the unicode character to indicate EOF
. I really don’t know what you are trying to achieve in your code, but I can help you fix it.
In my knowledge, x1a
takes 1 length in the str
, so removing it would be easy as it always be at the end of the matrix
.
matrix = matrix.strip().split('n')
matrix = [x if 'x1a' not in x else x[:len(x)-1] for x in matrix]
It checks for x1a
, if it is found, it removes it from the matrix
.
Output (It also includes the edited matrix
, or to be exact newly created matrix
):
['1 2', '3 4']
Data Loaded Sucessfully
0
0 3
1 4
def load_matrix():
matrix = sys.stdin.read()
matrix = matrix.strip().split('n')
matrix = [row.split(' ') for row in matrix]
for m in matrix:
matrix=[int(l) for l in m]
print("n Data Loaded Sucessfully")
print("n")
var=pd.DataFrame(matrix)
return var
Look at this piece of code. I am trying to take a matrix directly as the input for the program, but the read function takes the input as a list of strings. To convert it into a int object for performing math functions, i have used the code above. running fine with online compilers but VS Code keeps throwing an error saying:
ValueError: invalid literal for int() with base 10: ‘4x1a’
(when given the input as:-
This )
From what I have researched, this happens when the value passed cannot be converted into an integer.
When I run a similar code in Jupyter notebook it runs smoothly.
Your Help will be appreciated.
As pointed in the comments, x1a
is the unicode character to indicate EOF
. I really don’t know what you are trying to achieve in your code, but I can help you fix it.
In my knowledge, x1a
takes 1 length in the str
, so removing it would be easy as it always be at the end of the matrix
.
matrix = matrix.strip().split('n')
matrix = [x if 'x1a' not in x else x[:len(x)-1] for x in matrix]
It checks for x1a
, if it is found, it removes it from the matrix
.
Output (It also includes the edited matrix
, or to be exact newly created matrix
):
['1 2', '3 4']
Data Loaded Sucessfully
0
0 3
1 4