TypeError:需要类似字节的对象,而不是'str'(文件打开)

时间:2017-09-21 14:10:51

标签: string byte python-3.6

我没有成功添加“b”来读取所有字符串前面的字节,即使它应该根据许多旧线程来解决问题。 这是我的代码:

# -*- coding: utf-8 -*-
__author__ = 'chrispaulson'
import numpy as np
import math as m
import os
import pickle
import pyKriging


class samplingplan():


    def optimallhc(self,n,population=30, iterations=30, generation=False):

            if not generation:
                # Check for existing LHC sampling plans
                if os.path.isfile(b'{0}lhc_{1}_{2}.pkl'.format(self.path,self.k, n)):
                    # codecs.open(filename,'r',encoding='utf8')
                    X = pickle.load(open(b'{0}lhc_{1}_{2}.pkl'.format(self.path,self.k, n), 'rb'))
                    return X
                else:
                    print(self.path)
                    print('SP not found on disk, generating it now.')

在主代码中引用“sampleplan()”类的函数“optimallhc”时

import pyKriging
from pyKriging.krige import kriging  
from pyKriging.samplingplan import samplingplan

# The Kriging model starts by defining a sampling plan, we use an optimal Latin Hypercube here
sp = samplingplan(2)  
X = sp.optimallhc(20)

它返回以下回溯消息:

Traceback (most recent call last):

  File "<ipython-input-1-e9ff50d9f93a>", line 1, in <module>
    runfile('C:/Andreas Luckert/Kriging-plot-Ehingen.py', wdir='C:/Andreas Luckert')

  File "C:\Users\Müller\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 710, in runfile
    execfile(filename, namespace)

  File "C:\Users\Müller\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 101, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Andreas Luckert/Kriging-plot-Ehingen.py", line 14, in <module>
    X = sp.optimallhc(20)

  File "C:\Users\Müller\Anaconda3\lib\site-packages\pyKriging\samplingplan.py", line 72, in optimallhc
    if os.path.isfile(b'{0}lhc_{1}_{2}.pkl'.format(self.path,self.k, n)):

AttributeError: 'bytes' object has no attribute 'format'

如上所述,我已经在各处添加了字节处理的“b”,即使在其他组合中也是如此,但它从未奏效。 如果有人知道怎么办,我会非常感激。 提前谢谢!

0 个答案:

没有答案