You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用NumPy读取FIFA 18球员数据集时Python 3报UnicodeDecodeError

Fixing UnicodeDecodeError with np.genfromtxt for FIFA 18 Dataset

Hey there! That UnicodeDecodeError is super common when working with CSV datasets that aren’t saved in the default UTF-8 encoding—and the FIFA 18 player dataset is a classic example of this. Let’s break down how to fix it quickly.

Why this happens

NumPy’s np.genfromtxt() defaults to using UTF-8 encoding, but many older datasets (like this FIFA one) are saved with Latin-1 (or cp1252) encoding instead. The error pops up when the file has characters that don’t fit in UTF-8.

The quick fix

Just add the encoding parameter to your genfromtxt call, specifying either 'latin1' or 'cp1252'—both should work for this dataset:

import numpy as np
# Use latin1 encoding to handle non-UTF-8 characters
np_fifa = np.genfromtxt('Datasets/FIFA2018.csv', delimiter=',', encoding='latin1')
print(np_fifa)

If that still doesn’t work...

If you’re still getting errors, you can check the actual encoding of your CSV file:

  • Open it in a text editor like Notepad++ and look at the "Encoding" menu to see what it’s saved as.
  • Or use the chardet library to auto-detect the encoding:
    import chardet
    with open('Datasets/FIFA2018.csv', 'rb') as f:
        result = chardet.detect(f.read())
    print(result['encoding'])
    

Then use that detected encoding in the encoding parameter.

内容的提问来源于stack exchange,提问作者user8795229

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:17:23