Home > Enterprise >  data is different when printing vs asssigning as a variable
data is different when printing vs asssigning as a variable

Time:01-10

I am looping through a json array grabbing 2 data keys, displayName and currency. If i print the data it prints the correct data but when I assign it as a variable the data is different than what is printed.

import json
from firebotpy import firebot



def write(data):
    f = open('data.txt', 'a')
    f.write(f"{data}"'\n')

with open('data.json', encoding= 'utf-8') as f:
    data = json.load(f)
    user_data = data['userData']
    x = user_data.keys()
    for user in x:
        try:
            print(user_data[user]['displayName'])
            print(user_data[user]['currency'])
            user = user_data[user]['displayName']
            value = user_data[user]['currency']
            print(user)
            print(value)
        except:
            pass

now when I print the data it prints the correct data but when i assign it to a variable it does not. What am I doing wrong?

TERMINAL when printing the daya, first two lines of code

30
gggymm
151
arturo_544
40
megan3308
37
ryunjin_98
241
xxpainlovexx
129
ヤン얀
18
leominakata
16
jairock08
11
antiheroe00
6

TERMINAL when assinging the data to a variable and printing it

2
spawned55
2
spawned56
2
spawned57
2
spawned58
2
spawned59
2
spawned60
2
spawned61
2
spawned62
2
spawned63
2
spawned64
2
spawned65
2
spawned66
2
spawned67
2
spawned68
2
spawned69
2

CodePudding user response:

Your question isn't entirely clear from the sample inputs and outputs, but I assume you're asking why the entries in the array aren't changing even after assigning a new value to user. Your code implies you want user to become just the display name key of the user dict, so here's my explanation:


The user variable in the for loop stores a value. This might sound obvious, but there's a catch in the simplicity: The variable stores the value of the array element, not the element itself.

So when you assign a value to the variable user, it won't update the array itself. It just updates the value of the variable user for the remainder of the loop body, and in the next iteration the variable user is changed to the value of the next array element.

The solution would be:

user_data[user] = user_data[user]['displayName']

Which will change the array element itself.

CodePudding user response:

Without seeing how your data is structured, I cant really do too much to help.

But from what I understand, this might help solve the problem.

data = {'userData': [{'displayName': 'Test1', 'currency': 50}, {'displayName': 'Test2', 'currency': 65}]}

for x in data['userData']:
    for key, value in x.items():
        try:
            # use key and value variables how ever you like here
            print(key)
            print(value)
        except:
            pass

or

for x in data['userData']:
    name = x.get('displayName')
    currency = x.get('currency')

    print(name)
    print(currency)
  •  Tags:  
  • Related