python - 使用 Python 将 .arff 文件转换为 .csv

标签 python csv arff

我有一个文件“LMD.rh.arff”,我正尝试使用以下代码将其转换为 .csv 文件-

import pandas as pd
import matplotlib.pyplot as plt
from scipy.io import arff


# Read in .arff file-
data = arff.loadarff("LMD.rh.arff")

但是最后一行代码给了我错误-

--------------------------------------------------------------------------- UnicodeEncodeError Traceback (most recent call last) in ----> 1 data = arff.loadarff("LMD.rp.arff")

~/.local/lib/python3.6/site-packages/scipy/io/arff/arffread.py in loadarff(f) 539 ofile = open(f, 'rt') 540 try: --> 541 return _loadarff(ofile) 542 finally: 543 if ofile is not f: # only close what we opened

~/.local/lib/python3.6/site-packages/scipy/io/arff/arffread.py in _loadarff(ofile) 627 a = generator(ofile) 628 # No error should happen here: it is a bug otherwise --> 629 data = np.fromiter(a, descr) 630 return data, meta 631

UnicodeEncodeError: 'ascii' codec can't encode character '\xf3' in position 4: ordinal not in range(128)

In [6]: data = arff.loadarff("LMD.rh.arff")

--------------------------------------------------------------------------- UnicodeEncodeError Traceback (most recent call last) in ----> 1 data = arff.loadarff("LMD.rh.arff")

~/.local/lib/python3.6/site-packages/scipy/io/arff/arffread.py in loadarff(f) 539 ofile = open(f, 'rt') 540 try: --> 541 return _loadarff(ofile) 542 finally: 543 if ofile is not f: # only close what we opened

~/.local/lib/python3.6/site-packages/scipy/io/arff/arffread.py in _loadarff(ofile) 627 a = generator(ofile) 628 # No error should happen here: it is a bug otherwise --> 629 data = np.fromiter(a, descr) 630 return data, meta 631

UnicodeEncodeError: 'ascii' codec can't encode character '\xf3' in position 4: ordinal not in range(128)

您可以下载文件arff_file

有什么问题吗?

谢谢!

最佳答案

试试这个

path_to_directory="./"
files = [arff for arff in os.listdir(path_to_directory) if arff.endswith(".arff")]

def toCsv(content): 
    data = False
    header = ""
    newContent = []
    for line in content:
        if not data:
            if "@attribute" in line:
                attri = line.split()
                columnName = attri[attri.index("@attribute")+1]
                header = header + columnName + ","
            elif "@data" in line:
                data = True
                header = header[:-1]
                header += '\n'
                newContent.append(header)
        else:
            newContent.append(line)
    return newContent

# Main loop for reading and writing files
for zzzz,file in enumerate(files):
    with open(path_to_directory+file , "r") as inFile:
        content = inFile.readlines()
        name,ext = os.path.splitext(inFile.name)
        new = toCsv(content)
        with open(name+".csv", "w") as outFile:
            outFile.writelines(new)

关于python - 使用 Python 将 .arff 文件转换为 .csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55653131/

相关文章:

java - 使用 Weka Java 代码 - 如何将 CSV(无标题行)转换为 ARFF 格式?

java - FastVector<E> 类型已弃用

python - 保存 networkx 有向图,保留节点属性

python - 从服务器发送消息时出现 SocketIO "packet queue is empty, aborting” 错误

python - 帮助解决 Python 循环怪异问题?

python - 在 Python 中将 .csv 值作为单个列表导入

java - 使用 Java 代码向 ARFF 添加问号

python - 使用 RandomForestClassifier 的 Scikit-learn MemoryError

python - 类型错误 : a bytes-like object is required, 不是 'int' python3

将数据写入csv的Python程序取决于列是否存在