为什么这段代码 运行 在 Jupyter 中可以正常运行,但在 IDLE 中却不行?

Why does this code run fine in Jupyter but not in IDLE?

我已经制作了这个 PDF 抓取工具,我可以在 Juypter notebook 中 运行 正常,但是当我将它移至 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

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

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

pip install tabula-py

是正确的方法。

你不应该做的

pip install tabula