Hello I have working code like this:
JavaScript
x
34
34
1
import pandas as pdfrom pandas.io.json import json_normalize
2
import json
3
import warnings
4
warnings.filterwarnings('ignore')
5
6
with open('yieldfull.json') as file:
7
data = json.load(file)
8
9
df_json = json_normalize(data)
10
df_json_stripped = data[0]
11
platform_dict = df_json_stripped['result']
12
platform_names = []
13
14
for key in platform_dict:
15
platform_names.append(key)
16
17
for name in platform_names:
18
if name == 'Autofarm':
19
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']
20
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']
21
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']
22
df_json = pd.DataFrame({'Vault_Name':vault_name_df, 'Current_Token_0':current_token_0 , 'Current_Token_1':current_token_1})
23
df_json.to_excel('Output_'+name+'.xlsx', index = False)
24
platform_names.remove(name)
25
elif name == 'Acryptos':
26
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']
27
price_USD = json_normalize(pd.DataFrame(dict([(k , pd.Series(v)) for k,v in df_json['result.'+name+'.vaults.vaults'].items()]))[0])['priceInUSDDepositToken']
28
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']
29
deposited_token = json_normalize(pd.DataFrame(dict([(k, pd.Series(v)) for k,v in df_json['result.'+name+'.vaults.vaults'].items()]))[0])['depositedTokens']
30
df_json = pd.DataFrame({'Vault_Name':vault_name_df, 'Price_USD':price_USD, 'Current_Token_0':current_token_0, 'Deposited_Token':deposited_token})
31
df_json.to_excel('Output_'+name+'.xlsx', index = False)
32
else:
33
pass
34
Problem is: If I leave it like this it only outputs for first if
. When I comment out that if
section it will successfully output elif
, but I can’t get it to output 2 files whatever I do. Any ideas?
Error I’m getting for Acryptos:
JavaScript
1
20
20
1
Traceback (most recent call last):
2
File "C:UsersAdamPycharmProjectsScrapy_Thingsvenvlibsite-packagespandascoreindexesbase.py", line 3080, in get_loc
3
return self._engine.get_loc(casted_key)
4
File "pandas_libsindex.pyx", line 70, in pandas._libs.index.IndexEngine.get_loc
5
File "pandas_libsindex.pyx", line 101, in pandas._libs.index.IndexEngine.get_loc
6
File "pandas_libshashtable_class_helper.pxi", line 4554, in pandas._libs.hashtable.PyObjectHashTable.get_item
7
File "pandas_libshashtable_class_helper.pxi", line 4562, in pandas._libs.hashtable.PyObjectHashTable.get_item
8
KeyError: 'result.Acryptos.vaults.vaults'
9
10
The above exception was the direct cause of the following exception:
11
12
Traceback (most recent call last):
13
File "C:/Users/Adam/PycharmProjects/Scrapy_Things/yieldwatch/yieldwatch/spiders/JsonExcel.py", line 27, in <module>
14
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']
15
File "C:UsersAdamPycharmProjectsScrapy_Thingsvenvlibsite-packagespandascoreframe.py", line 3024, in __getitem__
16
indexer = self.columns.get_loc(key)
17
File "C:UsersAdamPycharmProjectsScrapy_Thingsvenvlibsite-packagespandascoreindexesbase.py", line 3082, in get_loc
18
raise KeyError(key) from err
19
KeyError: 'result.Acryptos.vaults.vaults'
20
But if I comment out Autofarm and just process if for Acryptos is outputs excel just fine.
Advertisement
Answer
please remove the below line from your code
JavaScript
1
2
1
platform_names.remove(name)
2
debug code:
JavaScript
1
8
1
platform_names=['Autofarm','Acryptos']
2
for name in platform_names:
3
if name == 'Autofarm':
4
print("Autofarm")
5
#platform_names.remove(name) # remove this line
6
elif name == "Acryptos":
7
print("Acryptos")
8
you have initially created
df_json = json_normalize(data)
and also in loop, you are overwriting it –>
JavaScript
1
3
1
df_json = pd.DataFrame({'Vault_Name':vault_name_df, 'Current_Token_0':current_token_0 , 'Current_Token_1':current_token_1})
2
df_json.to_excel('Output_'+name+'.xlsx', index = False)
3
so change the name in loop and it will be okay.