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

2024-06-10 04:21:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是第三方模块,调用这些模块时出错。 以下是编译器显示的内容:

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

Tags: theinselfdatetimeifdtelsedate2num
1条回答
网友
1楼 · 发布于 2024-06-10 04:21:42
        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]

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->;csvidx。你知道吗

如果您的环境允许,可以使用python -u运行所有的东西,它使用无缓冲输出。(强烈建议在新行刷新不起作用或有问题的窗口下使用)

相关问题 更多 >