为什么这段代码在 Jupyter 中运行良好,但在 IDLE 中却没有?

时间:2021-06-21 12:28:52

标签: python jupyter python-idle

我制作了这个 PDF 抓取工具,可以在 Juypter 笔记本中正常运行,但是当我将其移至 IDLE 时,我在底部收到错误代码。没有关键错误,所以我不确定为什么没有打印结果!

我是新手,所以非常感谢任何帮助。

# In[46]:
import tabula
import pandas as pd


# In[52]:
URL = "http://ir.eia.gov/wpsr/overview.pdf"
table = tabula.read_pdf(URL,pages=1)
df = table[0]
df


# In[102]:
i = 4
result = []
while i < 23:
    phrase1 = df.iloc[i][2] 
    phrase2 = df.iloc[i][3]
    dot = phrase1.find(".")
    plus = int(dot) + 2
    left = phrase1[:dot]
    right = phrase1[dot:int(plus)]
    new_crude = (left + right)
    
    dot2 = phrase2.find(".")
    plus2 = int(dot) + 2
    left2 = phrase2[:dot2]
    right2 = phrase2[dot2:int(plus2)]
    old_crude = (left2 + right2)
    
    
    num = float(new_crude.replace(',','')) - float(old_crude.replace(',',''))
    result.append(round(num,2))
    
    i = i+1
result


# In[100]:
products = ["EIA (OCAL):",
"CRUDE:",
"GASOLINE:",
"DISTILLATE FUEL OIL:",
"PROPANE:"]
products


# In[109]:
print(products[0])
print(products[1]+str(result[1])+"m")
print(products[2]+str(result[3])+"m")
print(products[3]+str(result[9])+"m")
print(products[4]+str(result[14])+"m")


# In[ ]:
ERROR:
Traceback (most recent call last):
  File "C:\Users\cbullock\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\indexes\base.py", line 2895, in get_loc
    return self._engine.get_loc(casted_key)
  File "pandas\_libs\index.pyx", line 70, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 101, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1675, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1683, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 0

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\cbullock\AppData\Local\Programs\Python\Python39\EIA weekly - Automated (CB and EM).py", line 22, in <module>
    df = table[0]
  File "C:\Users\cbullock\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\frame.py", line 2906, in __getitem__
    indexer = self.columns.get_loc(key)
  File "C:\Users\cbullock\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\indexes\base.py", line 2897, in get_loc
    raise KeyError(key) from err
KeyError: 0

1 个答案:

答案 0 :(得分:2)

这是因为您使用 pip 安装了错误的软件包(使用 Jupyter 会自动安装正确的软件包)。

您需要安装tabula-py,而不是tabula

pip install tabula-py

是正确的方法。

你不应该这样做

pip install tabula