python - 数据输出和内部函数不一样
问题描述
我目前遇到一个问题,我试图将数据存储在列表中(使用数据类)。当我在函数 (PullIncursionData()) 的列表中打印数据时,它会以一定数量的数字进行响应(从不一样,由于它的性质是不可能的)。在调用它以存储它后打印它时,它以 Var 的形式返回,它以某种方式只打印相同的数字。
我无法分享这些数字,因为它们会使用 EVE Online 的 API 进行更新,所以唯一的方法是在本地运行它并自己阅读第一个列表。
存储库在这里:https ://github.com/AtherActive/EVEAPI-Demo
- 小心!在 main.py(有问题的文件)(下面是一段代码)内有更多功能。第 90 行及以后的所有函数都很重要,对于这个问题,其余的可以忽略,因为它们不与其他函数交互。
def PullIncursionData():
#Pulls data from URL and converts it into JSON
url = 'https://esi.evetech.net/latest/incursions/?datasource=tranquility'
data = rq.get(url)
jsData = data.json()
#Init var to store incursions
incursions = []
#Set lenght for loop. yay
length = len(jsData)
# Every loop incursion data will be read by __parseIncursionData(). It then gets added to var Incursions.
for i in range(length):
# Add data to var Incursion.
incursions.append(__parseIncursionData(jsData, i))
# If Dev mode, print some debug. Can be toggled in settings.py
if settings.developerMode == 1:
print(incursions[i].constellation_id)
return incursions
# Basically parses the input data in a decent manner. No comments needed really.
def __parseIncursionData(jsData, i):
icstruct = stru.Incursion
icstruct.constellation_id = jsData[i]['constellation_id']
icstruct.constellation_name = 'none'
icstruct.staging = jsData[i]['staging_solar_system_id']
icstruct.region_name = ResolveSystemNames(icstruct.constellation_id, 'con-reg')
icstruct.status = jsData[i]['state']
icstruct.systems_id = jsData[i]['infested_solar_systems']
icstruct.systems_names = ResolveSystemNames(jsData[i]['infested_solar_systems'], 'system')
return icstruct
# Resolves names for systems, regions and constellations. Still WIP.
def ResolveSystemNames(id, mode='constellation'):
#init value
output_name = 'none'
# If constellation, pull data and find region name.
if mode == 'con-reg':
url = 'https://www.fuzzwork.co.uk/api/mapdata.php?constellationid={}&format=json'.format(id)
data = rq.get(url)
jsData = data.json()
output_name = jsData[0]['regionname']
# Pulls system name form Fuzzwork.co.uk.
elif mode == 'system':
#Convert output to a list.
output_name = []
lenght = len(id)
# Pulls system name from Fuzzwork. Not that hard.
for i in range(lenght):
url = 'https://www.fuzzwork.co.uk/api/mapdata.php?solarsystemid={}&format=json'.format(id[i])
data = rq.get(url)
jsData = data.json()
output_name.append(jsData[i]['solarsystemname'])
return output_name
icdata = PullIncursionData()
print('external data check:')
length = len(icdata)
for i in range(length):
print(icdata[i].constellation_id)
结构.py(自定义文件)
@dataclass
class Incursion:
constellation_id = int
constellation_name = str
staging = int
staging_name = str
systems_id = list
systems_names = list
region_name = str
status = str
def ___init___(self):
self.constellation_id = -1
self.constellation_name = 'undefined'
self.staging = -1
self.staging_name = 'undefined'
self.systems_id = []
self.systems_names = []
self.region_name = 'undefined'
self.status = 'unknown'
解决方案
推荐阅读
- javascript - 当您有条件调用时,最好的休息 api 设计是什么?
- excel - 在文本框中输入特定文本并将 +1 添加到另一个单元格
- spartacus-storefront - 商品数据未更新
- firebase - Firebase:功能:上传错误:服务器错误。客户端网络套接字在建立安全 TLS 连接之前断开
- web - 建议对具有访问控制的选项卡进行 UX 改进
- php - Mailgun 电子邮件到处都被拒绝,但在 Gmail 上接收
- biztalk - BizTalk json 编码器未使用正确的类型
- python - 使python中的线程在到达函数末尾时自动加入
- php - 无法打开流:没有这样的文件或目录警告:require_once 致命错误:require_once():
- logic - 为什么 AND、OR 会按预期工作,但 XOR 会显示错误?