Pandas To_Excel 解析问题 - 仅输出 1 个文件

Pandas To_Excel parsing problem - outputting only 1 file

你好,我有这样的工作代码:

import pandas as pdfrom pandas.io.json import json_normalize
import json
import warnings
warnings.filterwarnings('ignore')

with open('yieldfull.json') as file:
    data = json.load(file)

df_json = json_normalize(data)
df_json_stripped = data[0]
platform_dict = df_json_stripped['result']
platform_names = []

for key in platform_dict:
    platform_names.append(key)

for name in platform_names:
    if name == 'Autofarm':
        vault_name_df = json_normalize(pd.DataFrame(dict([(k , pd.Series(v)) for k,v in df_json['result.'+name+'.LPVaults.vaults'].items()]))[0])['name']
        current_token_0 = json_normalize(pd.DataFrame(dict([(k , pd.Series(v)) for k,v in df_json['result.'+name+'.LPVaults.vaults'].items()]))[0])['LPInfo.currentToken0']
        current_token_1 = json_normalize(pd.DataFrame(dict([(k , pd.Series(v)) for k,v in df_json['result.'+name+'.LPVaults.vaults'].items()]))[0])['LPInfo.currentToken1']
        df_json = pd.DataFrame({'Vault_Name':vault_name_df, 'Current_Token_0':current_token_0 , 'Current_Token_1':current_token_1})
        df_json.to_excel('Output_'+name+'.xlsx', index = False)
        platform_names.remove(name)
    elif name == 'Acryptos':
        vault_name_df = json_normalize(pd.DataFrame(dict([(k , pd.Series(v)) for k,v in df_json['result.'+name+'.vaults.vaults'].items()]))[0])['name']
        price_USD = json_normalize(pd.DataFrame(dict([(k , pd.Series(v)) for k,v in df_json['result.'+name+'.vaults.vaults'].items()]))[0])['priceInUSDDepositToken']
        current_token_0 = json_normalize(pd.DataFrame(dict([(k , pd.Series(v)) for k,v in df_json['result.'+name+'.vaults.vaults'].items()]))[0])['currentTokens']
        deposited_token = json_normalize(pd.DataFrame(dict([(k, pd.Series(v)) for k,v in df_json['result.'+name+'.vaults.vaults'].items()]))[0])['depositedTokens']
        df_json = pd.DataFrame({'Vault_Name':vault_name_df, 'Price_USD':price_USD, 'Current_Token_0':current_token_0, 'Deposited_Token':deposited_token})
        df_json.to_excel('Output_'+name+'.xlsx', index = False)
    else:
        pass

问题是:如果我这样保留它,它只会输出第一个 if。当我注释掉 if 部分时,它将成功输出 elif,但无论我做什么,我都无法让它输出 2 个文件。有什么想法吗?

我获取 Acryptos 时出错:

Traceback (most recent call last):
  File "C:\Users\Adam\PycharmProjects\Scrapy_Things\venv\lib\site-packages\pandas\core\indexes\base.py", line 3080, 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 4554, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 4562, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'result.Acryptos.vaults.vaults'

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

Traceback (most recent call last):
  File "C:/Users/Adam/PycharmProjects/Scrapy_Things/yieldwatch/yieldwatch/spiders/JsonExcel.py", line 27, in <module>
    vault_name_df = json_normalize(pd.DataFrame(dict([(k , pd.Series(v)) for k,v in df_json['result.'+name+'.vaults.vaults'].items()]))[0])['name']
  File "C:\Users\Adam\PycharmProjects\Scrapy_Things\venv\lib\site-packages\pandas\core\frame.py", line 3024, in __getitem__
    indexer = self.columns.get_loc(key)
  File "C:\Users\Adam\PycharmProjects\Scrapy_Things\venv\lib\site-packages\pandas\core\indexes\base.py", line 3082, in get_loc
    raise KeyError(key) from err
KeyError: 'result.Acryptos.vaults.vaults'

但是如果我注释掉 Autofarm 并且只处理 Acryptos 的输出 excel 就好了。

请从您的代码中删除以下行

platform_names.remove(name)

调试代码:

platform_names=['Autofarm','Acryptos']
for name in platform_names:
    if name == 'Autofarm':
        print("Autofarm")
        #platform_names.remove(name) # remove this line
    elif name == "Acryptos":
        print("Acryptos")

您已初步创建

df_json = json_normalize(data)

并且在循环中,您正在覆盖它 -->

df_json = pd.DataFrame({'Vault_Name':vault_name_df, 'Current_Token_0':current_token_0 , 'Current_Token_1':current_token_1}) 
df_json.to_excel('Output_'+name+'.xlsx', index = False) 

所以在循环中更改名称就可以了。