Reputation: 41
I am still noob at 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)
Above is an OOP loop I am trying to build. Class and self is working perfectly, but when I append values to list and write it to "incubators.txt" it only writes values like:
[<__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>]
When printing self.name, self.exaltedValue or self.chaosValue I can see that I manage to fetch strings and integers but can only write objects to the file. How can I also write that strs and ints to the file? Thanks for any help.
Upvotes: 0
Views: 73
Reputation:
You can convert your list to JSON format and then write to the file.
Use this snippet :
result=json.dumps(list, default=lambda o: o.__dict__)
Then, you can then write the result in the file
Upvotes: 1