1

我想将原始 MNIST 数据库转换为 csv 文件。我使用了来自https://pjreddie.com/projects/mnist-in-csv/的代码

但是我得到了一些关于 ord() 的错误,我知道 ord() 应该以一个字符串作为元素,但我不知道为什么会发生这种情况,但不了解原始数据库的结构。


def convert(imgf, labelf, outf, n):
    f = open(imgf, "rb")
    o = open(outf, "w")
    l = open(labelf, "rb")

    f.read(16)
    l.read(8)
    images = []

    for i in range(n):
        image = [ord(l.read(1))]
        for j in range(28*28):
            image.append(ord(f.read(1)))
        images.append(image)

    for image in images:
        o.write(",".join(str(pix) for pix in image)+"\n")
    f.close()
    o.close()
    l.close()

convert("train-images-idx3-ubyte.gz", "train-labels-idx1-ubyte.gz","mnist_train.csv", 60000)
convert("t10k-images-idx3-ubyte.gz", "t10k-labels-idx1-ubyte.gz","mnist_test.csv", 10000)

这是我收到的错误消息

ord() expected a character, but string of length 0 found

4

0 回答 0