python在replace中需要一个整数

时间:2015-08-03 23:12:02

标签: python python-2.7

这是我的代码

for date in self.getDateRange():
          date = date.replace('-','/')

这是getDateRange函数:

def getDateRange(self):
    from datetime import date, datetime, timedelta
    return self.perdelta(date(2000, 01, 01), date(2015, 8, 03), timedelta(days=1))

def perdelta(self, start, end, delta):
    curr = start
    while curr < end:
        yield curr
        curr += delta

这是错误消息

MySpider.py", line 19, in parse
            date = date.replace('-','/')
        exceptions.TypeError: an integer is required

很奇怪,我总是可以做替换,没有任何问题,我不知道为什么会在这里

2 个答案:

答案 0 :(得分:4)

您正在对have their own replace() method

的实际 function ab2str(buf) { return String.fromCharCode.apply(null, new Uint16Array(buf)); } //converts a forge 0.6.x string of bytes to an ArrayBuffer function str2ab(str) { var b = new ArrayBuffer(str.length); var view = new Uint8Array(b); for(var i = 0; i < str.length; ++i) { view[i] = str.charCodeAt(i); } return b; } function _arrayBufferToBase64( buffer ) { var binary = ''; var bytes = new Uint8Array( buffer ); var len = bytes.byteLength; for (var i = 0; i < len; i++) { binary += String.fromCharCode( bytes[ i ] ); } return window.btoa( binary ); } var pubKey; var privKey; var s_pubKey="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA5tieEkcDAqAp/baSmNIdg5ezJMcWJX63+hM/DQolgKtw9Dvc0c8GRUqK2r/idZ1iXJzOjWQ3KIFIzmgTRHOKe3aPgJADdZ2vyAqu2woKtsgQ8nTcDYI86dmyfYsHFbgbSn/qeoE8WEzwyT3OoGgn54zoMuQmUmLbGaJYY2XN5bxwqxsRJSoXetBu9o1G9Wy4V1fdxwjRtaO/2FrZfkLp/P5Tc5Hk1Ev7PIPrkRhrl/7lF4JblVRG5m90aeviErvFIN0LdlqiY90UaQY3gyTsylrheTlqRq6yyzKf3dWnF78+CeAqppsOMI+WHURThNLkN56EOTX6TaBrG6f2XjxeqQIDAQAB"; var cripto= window.crypto || window.msCrypto; var subtle= cripto.subtle || window.crypto.webkitSubtle; subtle.generateKey( { name: "RSA-OAEP", modulusLength: 2048, //can be 1024, 2048, or 4096 publicExponent: new Uint8Array([0x01, 0x00, 0x01]), hash: {name: "SHA-256"}, //can be "SHA-1", "SHA-256", "SHA-384", or "SHA-512" }, true, //whether the key is extractable (i.e. can be used in exportKey) ["encrypt", "decrypt"] //must contain both "encrypt" and "decrypt" ) .then(function(key){ //returns a keypair object //console.log(key); //console.log(key.publicKey); //console.log(key.privateKey); subtle.exportKey( "jwk", //can be "jwk" (public or private), "spki" (public only), or "pkcs8" (private only) key.publicKey //can be a publicKey or privateKey, as long as extractable was true ) .then(function(keydata){ //returns the exported key data console.log("publicKey:"+_arrayBufferToBase64(keydata)); subtle.exportKey( "jwk", //can be "jwk" (public or private), "spki" (public only), or "pkcs8" (private only) key.privateKey //can be a publicKey or privateKey, as long as extractable was true ) .then(function(keydata){ //returns the exported key data console.log("privateKey:"+_arrayBufferToBase64(keydata)); }); subtle.importKey( "jwk", //can be "jwk" (public or private), "spki" (public only), or "pkcs8" (private only) keydata, { //these are the algorithm options name: "RSA-OAEP", hash: {name: "SHA-256"}, //can be "SHA-1", "SHA-256", "SHA-384", or "SHA-512" }, false, //whether the key is extractable (i.e. can be used in exportKey) ["encrypt"] //"encrypt" for public key import, "decrypt" for private key imports ) .then(function(publicKey){ //returns a publicKey (or privateKey if you are importing a private key) console.log(publicKey); subtle.encrypt( { name: "RSA-OAEP", //label: Uint8Array([...]) //optional }, publicKey, //from generateKey or importKey above str2ab("hola mundo") //ArrayBuffer of data you want to encrypt ) .then(function(encrypted){ //returns an ArrayBuffer containing the encrypted data //console.log(new Uint8Array(encrypted)); console.log("enc: "+_arrayBufferToBase64(encrypted)); }) .catch(function(err){ console.error(err); }); }) }) }) .catch(function(err){ console.error(err); }); 个对象进行操作
date
     

返回具有相同值的日期,但通过指定的任何关键字参数给定新值的参数除外。例如,如果d == date(2002,12,31),那么d.replace(day = 26)== date(2002,12,26)。

这种方法只需要整数。如果要将日期作为字符串处理,则需要转换它们。但是,由于您已有对象,因此可以使用斜杠作为分隔符使用date.replace(year, month, day) 对其进行格式化。

答案 1 :(得分:0)

列表中的对象不是字符串。它们是日期时间对象(https://docs.python.org/2/library/datetime.html)。

您应该使用date.strftime以您喜欢的任何格式输出日期。有关完整格式选项,请参阅https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior

例如,您可能需要date.strftime('%m /%d /%Y')