我仍然是 OOP 的菜鳥。
class ItemPrices:
def __init__(self, name=" ", exval=0, chval=0, curtype= " "):
self.name = name
self.exaltedValue = exval
self.chaosValue = chval
self.currType = curtype
def Incubator_Values(self):
response_API = requests.get("https://poe.ninja/api/data/itemoverview?league=Scourge&type=Incubator")
data = response_API.text
os.chdir(
r"C:\Users\emosc\PycharmProjects\GithubPushs\psychescape_price_fetcher\psychescape_price_fetcher\values")
parse_json = json.loads(data)
with open("incubators.txt", "w") as file:
file.write(" ")
j = 0
for i in parse_json["lines"]:
list = []
if (i["chaosValue"] > chaos_ex_ratio):
self.name = i["name"]
self.exaltedValue = i["exaltedValue"]
self.currType = "Exalted Orb"
print(self.name)
list.append(self)
j = j 1
print("Current iteration> ", j)
else:
self.name = i["name"]
self.exaltedValue = i["chaosValue"]
self.currType = "Chaos Orb"
print(self.name)
list.append(self)
j = j 1
print("Current iteration> ", j)
os.chdir(
r"C:\Users\emosc\PycharmProjects\GithubPushs\psychescape_price_fetcher\psychescape_price_fetcher\values")
with open("incubators.txt", "a") as file:
file.write(str(list))
file.write("\n")
file.close()
print("Incubator values has now storaged at IncubatorValues class and imported to incubators.txt")
print(self)
以上是我正在嘗試構建的 OOP 回圈。類和自我作業正常,但是當我將值附加到串列并將其寫入“incubators.txt”時,它只寫入如下值:
[<__main__.ItemPrices object at 0x0000023A951ACFD0>]
[<__main__.ItemPrices object at 0x0000023A951ACFD0>]
[<__main__.ItemPrices object at 0x0000023A951ACFD0>]
[<__main__.ItemPrices object at 0x0000023A951ACFD0>]
[<__main__.ItemPrices object at 0x0000023A951ACFD0>]
列印 self.name、self.exaltedValue 或 self.chaosValue 時,我可以看到我設法獲取字串和整數,但只能將物件寫入檔案。我怎樣才能將 strs 和 int 寫入檔案?謝謝你的幫助。
uj5u.com熱心網友回復:
您可以將串列轉換為 JSON 格式,然后寫入檔案。
使用這個片段:
result=json.dumps(list, default=lambda o: o.__dict__)
然后,您可以將結果寫入檔案
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/418634.html
標籤:
上一篇:我怎樣才能使它更簡單易讀?在我的井字游戲python專案中
下一篇:如何從訊息中洗掉特定用戶的反應?
