Python 将MNIST数据库转换为.csv:ord()时出错
我想将原始MNIST数据库转换为csv文件。 我使用的代码来自 但是我对ord()有一些错误 我知道ord()应该使用一个字符串作为元素,但如果不了解原始数据库的结构,我不知道为什么会发生这种情况Python 将MNIST数据库转换为.csv:ord()时出错,python,mnist,ord,Python,Mnist,Ord,我想将原始MNIST数据库转换为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) imag
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()需要一个字符,但找到长度为0的字符串
,可能是因为l.read(1)
返回了一个空字符串,这意味着您已经到达了文件的末尾。@Thierrylahuille我不这么认为,因为我们使用了范围(n)其中不包括文件的结尾可能尝试用28*28-1
替换28*28-1
,因为您已经read(1)
@benoitdementhire它不起作用,显示相同的错误。要调试,请尝试检查文件的长度并计算调用read的次数,可能是因为l.read(1)
返回了一个空字符串,这意味着您已经到达了文件的末尾。@Thierrylahuille我不这么认为,因为我们使用了不包括文件末尾的范围(n),可能尝试用28*28-1
替换28*28-1
,因为您已经读取了(1)
@benoitdementhire它不起作用,显示相同的错误。若要调试,请尝试检查文件的长度并计算调用read的次数