Im trying to get this JSON data to print counting up on different items from 0 until it runs out of items since it will be different for each build. I have 2 different codes I have the code without the while loop which doesnt give me and errors and runs how id like but it doesnt count up it just shows the first value, and then I have the code with the while loop that gives me the error when I try to count up.
No While Loop Code:
import json
import xmltodict
import time
url = "https://pobb.in/DrX67Cqoz5cV/xml"
response = requests.get(url)
def xmlToJson():
with open ("build.xml", "w") as xml_file:
xml_file.write(response.text)
with open("build.xml") as xml_file:
data_dict = xmltodict.parse(xml_file.read())
json_data = json.dumps(data_dict)
with open("build.json", "w") as json_file:
json_file.write(json.dumps(json.loads(json_data), indent=4))
#xmlToJson()
def findUniques():
with open('build.json', "r") as f:
data = json.loads(f.read())
x=0
try:
#data = data['PathOfBuilding']['Items']['Item'][integer]['#text']
data = data['PathOfBuilding']['Items']['Item'][x]['#text']
print(data)
if "UNIQUE" in data:
print(f"Item Is Unique")
elif "RARE" in data:
print(f"Item Is Rare")
x+=1
except IndexError:
print("IndexError")
pass
findUniques()
While Loop Code:
import json
import xmltodict
import time
url = "https://pobb.in/DrX67Cqoz5cV/xml"
response = requests.get(url)
def xmlToJson():
with open ("build.xml", "w") as xml_file:
xml_file.write(response.text)
with open("build.xml") as xml_file:
data_dict = xmltodict.parse(xml_file.read())
json_data = json.dumps(data_dict)
with open("build.json", "w") as json_file:
json_file.write(json.dumps(json.loads(json_data), indent=4))
#xmlToJson()
def findUniques():
with open('build.json', "r") as f:
data = json.loads(f.read())
x=0
while True:
try:
#data = data['PathOfBuilding']['Items']['Item'][integer]['#text']
data = data['PathOfBuilding']['Items']['Item'][x]['#text']
print(data)
if "UNIQUE" in data:
print(f"Item Is Unique")
elif "RARE" in data:
print(f"Item Is Rare")
x+=1
except IndexError:
print("IndexError")
break
findUniques()
The Error I get:
File "g:CodingPython2024poetestpobtest.py", line 50, in <module>
findUniques()
File "g:CodingPython2024poetestpobtest.py", line 36, in findUniques
data = data['PathOfBuilding']['Items']['Item'][x]['#text']
~~~~^^^^^^^^^^^^^^^^^^
TypeError: string indices must be integers, not 'str'
I’ve tried changing the way I count up using different variables or using for loops instead of while loops but it seems if I add any sort of loop I get the error.
Any other code/information could be given if needed.