iteritems()与items()

@@ -178,7 +178,7 @@ class GenericRowParser(RowParser):

     # Process extra columns.
extra = {}

•    for k, v in csvRowDict.iteritems():

•    for k, v in csvRowDict.items():
if k not in self.__columnNames:
extra[k] = csvutils.float_or_string(v)


sort()的cmp参数

#python2以age排序
persons=[{‘name’:‘zhang3’,‘age’:15},{‘name’:‘li4’,‘age’:12}]
#升序
persons.sort(lambda a,b:cmp(a[‘age’],b[‘age’]))
#降序
persons.sort(lambda a,b:cmp(b[‘age’],a[‘age’]))

#python3以age排序
persons=[{‘name’:‘zhang3’,‘age’:15},{‘name’:‘li4’,‘age’:12}]
#升序
persons.sort(key = lambda a:a[‘age’])
#降序需要传参reverse的值为True
persons.sort(key = lambda a:a[‘age’],reverse=True)

@@ -68,15 +68,15 @@ class BarFeed(barfeed.BaseBarFeed):

     # Add and sort the bars
self.__bars[instrument].extend(bars)

•    barCmp = lambda x, y: cmp(x.getDateTime(), y.getDateTime())

•    self.__bars[instrument].sort(barCmp)

•    barCmp = lambda x : x.getDateTime()

•    self.__bars[instrument].sort(key=barCmp)

self.registerInstrument(instrument)


xrange()与range()

csv库里面的next()方法变更为__next__()
csv库在原python2中的next()方法被重命名为__next__()所以这里修改下名字就行了，具体修改代码如下：

@@ -31,23 +31,23 @@ class FastDictReader(object):
self.__fieldNames = fieldnames
if self.__fieldNames is None:

•        self.__fieldNames = self.reader.next()

•        self.__fieldNames = self.reader.__next__()
self.__dict = {}


def iter(self):
return self

• def next(self):
• def next(self):
# Skip empty rows.
•    row = self.reader.next()

•    row = self.reader.__next__()
while row == []:

•        row = self.reader.next()

•        row = self.reader.__next__()

# Check that the row has the right number of columns.
assert(len(self.__fieldNames) == len(row))

# Copy the row values into the dict.

•    for i in xrange(len(self.__fieldNames)):

•    for i in range(len(self.__fieldNames)):
self.__dict[self.__fieldNames[i]] = row[i]

return self.__dict


SMA

©️2019 CSDN 皮肤主题: 1024 设计师: 上身试试