def predict_encoding(file_path, n_lines=20):
    '''Get file encoding of a text file'''
    import chardet

    # Open the file as binary data
    with open(file_path, 'rb') as f:
        # Join binary lines for specified number of lines
        rawdata = b''.join([f.readline() for _ in range(n_lines)])

    return chardet.detect(rawdata)['encoding']
def predict_encoding ( file_path , n_lines = 20 ) : import chardet with open ( file_path , 'rb' ) as f : rawdata = b'' . join ( [ f . readline ( ) for _ in range ( n_lines ) ] ) return chardet . detect ( rawdata ) [ 'encoding' ]
Get file encoding of a text file