如何解决如何在python函数中使用列表对象作为参数?
我是编程新手,遇到以下问题。
我使用该函数的目标是将所有列表对象一一运行,并将数据保存为名为 erc20 的变量。
Link to .json file // Link to etherscan-python github
from etherscan import Etherscan
import json
with open('adress-tracker.json') as json_file:
json_data = json.load(json_file)
print(json_data)
# Here we create a result list,in which we will store our addresses
result_list = [json_dict['Address'] for json_dict in json_data]
eth = Etherscan("APIKEY") #removed my api key
erc20 = eth.get_erc20_token_transfer_events_by_address(address = result_list,startblock="0",endblock="999999999",sort='asc')
print(erc20)
这将返回以下错误:
AssertionError: Error! Invalid address format -- NOTOK
当我直接添加地址或将其链接到变量时,它工作得很好。但是我需要找到一种方法来将这些函数应用于所有地址,因为我计划添加数百个。
我尝试将列表更改为目录,并尝试使用 (*result_list) 实现关键字参数或创建一个名为 params 的新变量,其中包含所有需要的参数。然后使用 (*params)。但不幸的是,我无法解决如何解决这个问题。
在此先感谢您!
解决方法
此函数需要单个地址,因此您必须使用 #include <stdio.h>
#include <stdlib.h>
int length=4,width=4;
struct LandData
{
int height;
};
struct LandData* WritingData()
{
int i,j,op;
FILE *land;
struct LandData *arr = (struct LandData*)malloc(length* width* sizeof(struct LandData));
if((land=fopen("file.bin","rb"))==NULL)
{
perror("Can't open File");
}
if(!(fread(arr,sizeof(struct LandData),1,land)))
{
printf("Error file is empty!\n");
if((land = fopen("file.bin","wb")) == NULL)
{
printf("Unable to open file!\n");
exit(1);
}else printf("Opened file successfully.\n");
for (i = 0; i < length ; i++){
for (j = 0; j < width; j++){
printf("choose height: ");
scanf("%d",&(arr + i*width + j)->height);
fwrite(arr,land);
}
}
if(fclose(land)!=0)
{
perror("Error on file closing after writing");
exit(2);
}
}
return(arr);
}
void DisplayingMap(struct LandData *arr)
{
FILE* land;
if((land=fopen("file.bin","rb"))==NULL)
{
perror("Can't open File");
exit(1);
}
printf("\n ");
for (int i=0 ; i<width; i++)
printf("%d| ",i);
printf("\n");
while(fread(arr,land))
{
for (int i = 0; i < length ; i++) {
printf(" %d| ",i);
for (int j = 0; j < width; j++)
printf(" %d ",(arr + i*width + j)->height);
printf("\n");
}
if(fclose(land)!=0)
{
perror("Error on file closing after reading");
exit(2);
}
}
}
int main()
{
struct LandData *arr = WritingData();
DisplayingMap(arr);
}
循环分别检查每个地址
for
编辑:
对我有用的最少工作代码:
erc20 = []
for address in result_list:
result = eth.get_erc20_token_transfer_events_by_address(address=address,startblock="0",endblock="999999999",sort='asc')
erc20.append(result)
print(erc20)
结果:
import os
import json
from etherscan import Etherscan
TOKEN = os.getenv('ETHERSCAN_TOKEN')
eth = Etherscan(TOKEN)
with open('addresses.json') as json_file:
json_data = json.load(json_file)
#print(json_data)
erc20 = []
for item in json_data:
print(item['Name'])
result = eth.get_erc20_token_transfer_events_by_address(address=item['Address'],sort='asc')
erc20.append(result)
print('len(result):',len(result))
#print(erc20)
#for item in erc20:
# print(item)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。