【发布时间】:2016-11-09 09:09:43
【问题描述】:
我在 python3 中加载腌制文件时遇到问题。见以下代码:
#!/usr/bin/env python3
import csv,operator
import pickle
import os.path
pathToBin = "foo/bar/foobar.bin"
pathToCSV = "foo/bar/foobar.csv"
if os.path.isfile(pathToBin):
print("Binary file already on harddrive, loading from there")
transactions = pickle.loads( open( pathToBin, "rb" ))
else:
csvIn = open(pathToCSV,'r')
reader = csv.reader(csvIn)
header = next(reader)
header = header[0].split(";")
print("Reading file")
transactions = []
for row in reader:
# read file.
# transactions contains now lists of strings: transactions = [ ["a","b","c"], ["a2","b2","c3"], ...]
print("Dumping python file to harddrive")
myPickleFile = open(pathToBin,'wb')
pickle.dump(transactions, myPickleFile, protocol=pickle.HIGHEST_PROTOCOL)
# do some more computation
保存文件没有任何问题。但是,加载它会给我以下错误:
transactions = pickle.loads( open( pathToBin, "rb" ))
TypeError: '_io.BufferedReader' does not support the buffer interface
由于我使用的是 python3,因此对字符串的处理方式不同。因此,我专门为保存/加载提供了“b”选项。有人知道为什么这不起作用吗?
【问题讨论】:
标签: python-3.x pickle