使用第三方模块在Python中打印到控制台

时间:2017-10-26 20:57:28

标签: python printing

我使用第三方模块并在调用这些模块时发现错误。 这是编译器显示的内容:

C:\Users\Dmitry\AppData\Local\Enthought\Canopy\edm\envs\User\lib\site-    packages\backtrader\feeds\csvgeneric.py in _loadline(self, linetokens)
    148                 # get it from the token
    149                 csvfield = linetokens[csvidx]
--> 150                 print(csvidx)
    151 
    152             if csvfield == '':
IndexError: list index out of range

我故意添加了print(csvidx)以查看csvidx的值,但它没有显示在控制台上。我究竟做错了什么?非常感谢。

以下是代码:

  def _loadline(self, linetokens):
        # Datetime needs special treatment
        dtfield = linetokens[self.p.datetime]
        if self._dtstr:
            dtformat = self.p.dtformat

            if self.p.time >= 0:
                # add time value and format if it's in a separate field
                dtfield += 'T' + linetokens[self.p.time]
                dtformat += 'T' + self.p.tmformat

            dt = datetime.strptime(dtfield, dtformat)
        else:
            dt = self._dtconvert(dtfield)

        if self.p.timeframe >= TimeFrame.Days:
            # check if the expected end of session is larger than parsed
            if self._tzinput:
                dtin = self._tzinput.localize(dt)  # pytz compatible-ized
            else:
                dtin = dt

            dtnum = date2num(dtin)  # utc'ize

            dteos = datetime.combine(dt.date(), self.p.sessionend)
            dteosnum = self.date2num(dteos)  # utc'ize

            if dteosnum > dtnum:
                self.lines.datetime[0] = dteosnum
            else:
                # Avoid reconversion if already converted dtin == dt
                self.l.datetime[0] = date2num(dt) if self._tzinput else dtnum
        else:
            self.lines.datetime[0] = date2num(dt)

        # The rest of the fields can be done with the same procedure
        for linefield in (x for x in self.getlinealiases() if x != 'datetime'):
            # Get the index created from the passed params
            csvidx = getattr(self.params, linefield)

            if csvidx is None or csvidx < 0:
                # the field will not be present, assignt the "nullvalue"
                csvfield = self.p.nullvalue
            else:
                # get it from the token
                print(csvidx)
                csvfield = linetokens[csvidx]


            if csvfield == '':
                # if empty ... assign the "nullvalue"
                csvfield = self.p.nullvalue

            # get the corresponding line reference and set the value
            line = getattr(self.lines, linefield)
            line[0] = float(float(csvfield))

        return True

1 个答案:

答案 0 :(得分:0)

        csvidx = getattr(self.params, linefield)

        if csvidx is None or csvidx < 0:
            # the field will not be present, assignt the "nullvalue"
            csvfield = self.p.nullvalue
        else:
            # get it from the token
            print(csvidx)
            csvfield = linetokens[csvidx]
<{1}}正在csvidx寻找

self.params,显然已被发现。

似乎既不是None也不是< 0,所以它似乎有一个数值>= 0

IndexError: list index out of range清楚地表明linetokens不包含csvidx期望的项目。

由于名称self.params似乎表示用户输入,因此您提供的任何值似乎都大于linetokens中可用的实际令牌数

代码似乎是在其中一个固定的Python环境中执行的

--> 150                 print(csvidx)

因为这肯定不是通常的Python控制台输出。如果那个固定环境(而不是第三方软件包)确实允许你print到控制台,那么实际上可以更快地做到这一点似乎是可取的,如:

    for linefield in (x for x in self.getlinealiases() if x != 'datetime'):
        # Get the index created from the passed params
        csvidx = getattr(self.params, linefield)
        print('linefield {} -> csvidx {}'.format(linefield, csvidx)

        if csvidx is None or csvidx < 0:
            # the field will not be present, assignt the "nullvalue"
            csvfield = self.p.nullvalue
        else:
            # get it from the token
            csvfield = linetokens[csvidx]

您应该看到每个关系linefield - &gt;在触发异常之前csvidx

如果您的环境允许,请使用python -u运行使用无缓冲输出的所有内容。 (强烈建议在Windows下使用换行线冲洗不工作或有问题)