尝试使用 arff 读取文件时出错

问题描述 投票:0回答:0

我试图在 PyCharm 中运行这段代码,它非常简单,并且在 Jupyter Notebook 中运行得很好,但是当我将它带到 PyCharm 时,我得到一个我无法解决的错误。

有问题的代码是:

import pandas as pd


def load_kdd_dataset(data_path):
    """Lectura del conjunto de datos NSL-KDD."""
    with open(data_path, 'r') as train_set:
        dataset = arff.load(train_set)
    attributes = [attr[0] for attr in list(dataset)[1]["attributes"]]
    return pd.DataFrame(dataset["data"], columns=attributes)

df = load_kdd_dataset("C:/Users/negro/Desktop/Database/NSL-KDD/KDDTrain+.arff")

错误是:

C:\Users\negro\PycharmProjects\TstML3\venv\Scripts\python.exe C:\Users\negro\PycharmProjects\TstML3\main.py 
Traceback (most recent call last):
  File "C:\Users\negro\PycharmProjects\TstML3\main.py", line 12, in <module>
    df = load_kdd_dataset("C:/Users/negro/Desktop/Database/NSL-KDD/KDDTrain+.arff")
  File "C:\Users\negro\PycharmProjects\TstML3\main.py", line 9, in load_kdd_dataset
    attributes = [attr[0] for attr in list(dataset)[1]["attributes"]]
  File "C:\Users\negro\PycharmProjects\TstML3\venv\lib\site-packages\arff\__init__.py", line 239, in load
    with open(fname, 'r') as fhand:
TypeError: expected str, bytes or os.PathLike object, not TextIOWrapper

Process finished with exit code 1

解决方案是什么?谢谢。

我试图修复错误的路径,但事实并非如此。我不知道它可能是什么。

python jupyter-notebook pycharm
© www.soinside.com 2019 - 2024. All rights reserved.