python:ValueError:int()的基数为10的无效文字:''(python : ValueError: invalid literal for int() with base 10: ' ')
我有一个文本文件,其中包含条目
70154::308933::3 UserId::ProductId::Score
我把这个程序写成:(对不起,这里有点搞砸了)
def generateSyntheticData(fileName): dataDict = {} # rowDict = [] innerDict = {} try: # for key in range(5): # count = 0 myFile = open(fileName) c = 0 #del innerDict[0:len(innerDict)] for line in myFile: c += 1 #line = str(line) n = len(line) #print 'n: ',n if n is not 1: # if c%100 ==0: print "%d: "%c, " entries read so far" # words = line.replace(' ','_') words = line.replace('::',' ') words = words.strip().split() #print 'userid: ', words[0] userId = int( words[0]) # i get error here movieId = int (words[1]) rating =float( words[2]) print "userId: ", userId, " productId: ", movieId," :rating: ", rating #print words #words = words.replace('_', ' ') innerDict = dataDict.setdefault(userId,{}) innerDict[movieId] = rating dataDict[userId] = (innerDict) innerDict = {} except IOError as (errno,strerror): print "I/O error({0}) :{1} ".format(errno,strerror) finally: myFile.close() print "total ratings read from file",fileName," :%d " %c return dataDict
但我得到错误:
ValueError: invalid literal for int() with base 10: ''
有趣的是,它正好工作从其他文件中读取相同的格式数据。实际上在发布这个问题时,我注意到一些奇怪的事情。条目70154 :: 308933 :: 3每个数字之间都有一个像。空间0空间1空间5空间4空间::空间3 ... BUt文本文件看起来很好.. :(复制粘贴只显示这种性质..无论如何..但任何线索发生了什么。谢谢
I have a text file which contains entry like
70154::308933::3 UserId::ProductId::Score
I wrote this program to read: (Sorry the indendetion is bit messed up here)
def generateSyntheticData(fileName): dataDict = {} # rowDict = [] innerDict = {} try: # for key in range(5): # count = 0 myFile = open(fileName) c = 0 #del innerDict[0:len(innerDict)] for line in myFile: c += 1 #line = str(line) n = len(line) #print 'n: ',n if n is not 1: # if c%100 ==0: print "%d: "%c, " entries read so far" # words = line.replace(' ','_') words = line.replace('::',' ') words = words.strip().split() #print 'userid: ', words[0] userId = int( words[0]) # i get error here movieId = int (words[1]) rating =float( words[2]) print "userId: ", userId, " productId: ", movieId," :rating: ", rating #print words #words = words.replace('_', ' ') innerDict = dataDict.setdefault(userId,{}) innerDict[movieId] = rating dataDict[userId] = (innerDict) innerDict = {} except IOError as (errno,strerror): print "I/O error({0}) :{1} ".format(errno,strerror) finally: myFile.close() print "total ratings read from file",fileName," :%d " %c return dataDict
But i get the error:
ValueError: invalid literal for int() with base 10: ''
Funny thing is, it is working just fine reading the same format data from other file.. Actually while posting this question, I noticed something weird.. The entry 70154::308933::3 each number has a space.in between like 7 space 0 space 1 space 5 space 4 space :: space 3... BUt the text file looks fine..:( on copy pasting only it shows this nature.. Anyways.. but any clue whats going on. Thanks
原文:https://stackoverflow.com/questions/7439025
最满意答案
如果您希望登录同步运行:
var p = new jQuery.Deferred(); $.each(data, function(k,v){ p.then(function() { return login(v.u, v.p); }).then(doSomething).then(out); });
在
$.each
迭代的每个新项目都不会触发新的响应,直到p
完成最后一个。The idea is to create a recursive function like @Popnoodles have mentioned.
e.g.
function a() { return $.post(); } function b() { return $.post(); } function c() { console.log('no promise.'); } // and the recursive main function function main() { if(counter < data.length){ $.when(a().then(b).then(c)).done(function(){ counter++; main(); }); } } main();
Here is how it works, open the console to see how it logs the function in sequence.
相关问答
更多-
通过本地客户端程序获取服务器mysql数据库信息[2023-04-27]
ajax -
使用自定义对象 function DeferredAjax(opts) { this.options=opts; this.deferred=$.Deferred(); this.country=opts.country; } DeferredAjax.prototype.invoke=function() { var self=this, data={country:self.country}; console.log("Making request for [" ...
-
您可以使用.apply()和.apply()与多个延迟。 非常有用: function updateAllNotes() { var getarray = [], i, len; for (i = 0, len = data.length; i < len; i += 1) { getarray.push(getNote(data[i].key)); }; $.when.apply($, getarray).done(function() ...
-
呦! 解决了! http://jsfiddle.net/sandhyasriraj/AaHZv/ var x = null; var i = 0; x= $.Deferred(); var countries=["US","CA","MX","bx","fs","ZX"]; function log(msg) { var $out=$(""); $out.html(msg); $("#console").append($out); } callX = functio ...
-
有关jQuery的信息,请参阅kriskowal的Q库。 您可以将promises列表作为数组传递。 请参阅此页: https : //github.com/kriskowal/q/wiki/Coming-from-jQuery $.when(jQueryPromise1, jQueryPromise2).then(function (result1, result2) { }); // becomes Q.all([qPromise1, qPromise2]).spread(function (re ...
-
编辑而不是使用单独的延迟,您可以链接从getJSON()返回的那些 var getUsers = function() { var foo; return $.getJSON("http://codepen.io/chriscoyier/pen/EAIJj.js") .then(function(data) { foo = data; return $.getJSON("http://codepen.io/chriscoyier/pen/EAIJj.js") }).t ...
-
下面的示例显示了如何使用带函数的顺序调用。 而不是: when(singleCall()) ,就像when(singleCall) ; 没有parantheses。 或者,它将在promise解析之前执行该函数。 // just open up the network tab in your browser and see that // first 3 urls will go in parallel in threeCalls, // and then the last url will go in ...
-
jQuery $ .when不等待延期链完成ajax调用(jQuery $.when does not wait for deferred chain to complete ajax calls)[2023-04-05]
你的方法不起作用,因为你从同一个承诺中创建了两个分支。 你有相当于: var xxx = $.Deferred(); xxx.then(...).then(...); // promise chain 1 xxx.then(...) // promise chain 2 xxx.resolve(); // start the race between the two promise chains 这只是两个单独的承诺链之间的竞争,这些承诺链 ... -
如果您希望登录同步运行: var p = new jQuery.Deferred(); $.each(data, function(k,v){ p.then(function() { return login(v.u, v.p); }).then(doSomething).then(out); }); 在$.each迭代的每个新项目都不会触发新的响应,直到p完成最后一个。 The idea is to create a recursive function like @Popnoodles ...
-
使用Promises / A +,这很简单 promise.then(…, function(err) { if (checkIfContinuable(err)) return valueToConinueWith; else throw new TerribleError(err); }) 不幸的是, jQuery仍然不符合Promise / A + ,并转发旧值(结果或错误) - 除非你从回调中返回一个延迟的jQuery。 这与从成功处理程序拒绝的方式 ...