6

Working with python been banging my head for 2 days and keep getting this error. Tried fixed on Google but not one has worked.

Comments
  • 1
    Here's the code where the problem is at.
  • 1
    What does the dict contains before line 78?
  • 2
    @icycrash haven't take a deep look but that for loop looks like trouble.

    for json_dict in json_dict:

    Assign a different name to the element of json_dict.
  • 2
    for json_dict in json_dict:

    ...

    ......
  • 1
    It's misleading cause you named the var
    dict but it appears to have a string
  • 1
    for json_dict in json_dict:
    I dont belive that this is going to work. Use better variable names and don't recycle them like that
  • 2
    This bug should be fixable in waaaaaaay less time than 2 days. 1 look at it and it's clear:

    1. Your dict is not a dictionary. The error says so. Errors are rarely wrong.

    2. Did you forget to call something? What do you assign to it? A simple look through the code makes it obvious. But a proper editor can also help a lot. Nano is the worst editor for error checking.
  • 1
    I've changed json_dict to different things. Still same error.
  • 0
    A for loop with the iterable variable and the object your looping shouldn't have the same name. I've never done it but I'd assume that in memory you just overwrote the actual 'json_dict' object and changed it into the first item inside it. Then the next iteration it changes it again.

    So instead of a conveyor belt of products. The same product is being taken off the belt then put right back on

    EDIT: ohh just saw your new comment.
  • 0
    String indices must be integers
    json_dict is a string not a dict 🤷🏻‍♂️
  • 0
    I think the issue is the first block. You open a file as json_dict then you reassigned the variable again..maybe that could be it?
  • 2
    @Kimmax I tried it, while it's not fancy it should work l. However, looping over json_dict probably presents you a key first, instead loop over json_dict["maybe service_plans?"]
    That's the reason why you should use an ide, at least as long as you're learning. Or copy it over into a ide to test it instead of 2 days guess work. Hell, even print(json_dict) should be enough. If it's a string you know what's up
  • 1
    @YourNemesis and that's the correct interpretation of the error message. It's not saying "use integers as key", but "you have to use integers as 'key' (indices) when operating ON STRINGS"
  • 1
    @Kimmax ohh yea! I think you're right! Looping a dict does just give the key.
    To get the values you could do

    for i in list(dict):
    print dict[i]
  • 1
    for key, value in d.iteritems():

    There's this too @icycrash
  • 0
    You have to make a dict out of your string using
    json.loads
  • 0
    Here is similar code that runs before the piece that's broken that works.
  • 1
    @icycrash get a debugger or deliver facts about the state of the variables
  • 0
    @icycrash
    1. remove comments.
    2. rename all variables to use something other then json_dict. It is a dumb name, so stop using it.
    3. do not open database connection inside for loop. use with pattern when opening it.
    4. use filter, or reduce to get the keys/values you need from your file, and iterate over them using enumerate or any other pattern.

    the error you are seeing means that what you think is a dict/list is actually a string.
  • 1
    For your laptop sake please clean that screen 😱 argh....
  • 1
    json_dict is a string. It says clearely in the error...

    So if you have

    myString="abc"
    print myString["0"]

    that will cause such error. You should access with myString[0]
  • 1
    And for gods sake you aint using transistor so you have to reuse variable names. Feel free to name variables in a propper way
  • 0
    Thanks for all your help. I figured out my problem. Yes I have a database connection opened in a loop. The whole point of this script you have seen peices of pulls information out of ubiquiti's ucrm software using there API then takes key parts of it to inject into free radius SQL database to authenticate radios and que speeds for all 2500 clients.
  • 0
    @icycrash dude. please don't tell me this is used on a production system with 2.5k clients...
  • 0
    @magicMirror it is used to for the first sync so we don't have to insert the information twice. It's tested and worked with no errors
  • 0
    @magicMirror and honestly my boss wouldn't give two shits if it was a all the time thing. The cheap asshole is wanting this done to save money because the software is free.
Add Comment