将 OOP 对象转换为 Python 中的文本
Converting OOP objects to text in Python
我还是 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 循环。 Class 并且 self 工作得很好,但是当我
将值附加到列表并将其写入“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 和 ints 写入文件?感谢您的帮助。
您可以将列表转换为 JSON 格式,然后写入文件。
使用此代码段:
result=json.dumps(list, default=lambda o: o.__dict__)
然后,你就可以把结果写入文件了
我还是 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 循环。 Class 并且 self 工作得很好,但是当我 将值附加到列表并将其写入“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 和 ints 写入文件?感谢您的帮助。
您可以将列表转换为 JSON 格式,然后写入文件。
使用此代码段:
result=json.dumps(list, default=lambda o: o.__dict__)
然后,你就可以把结果写入文件了