Search code examples
pythonencodingvalueerror

ValueError when opening a file, strange symbols


I try to open a file where each contains data with this format :

1 5.4 93.6 1.0

I checked the whole file it is correct.

My code is :

with open('train2.txt') as file:
    data = np.matrix([[float(x) for x in line.split()] for line in file])

But I get the error :

ValueError: could not convert string to float: '1'

I first thought it was because my locals were in Japanese as there were japanese symbols instead of the strange ones. I tried to reencode the file after changing the locals back but the error is still there.


Solution

  • I run into this it was not getting me any error

    import numpy as np
    with open('file.txt') as file:
         data = np.matrix([[float(x) for x in line.split(' ')] for line in file])
         print(data)