0

I've searched around and tried to .encode(...) the string, but I can't imagine this issues is as serious as it seems.

I have two datastructures, one is loaded from a YAML file, the other from a JSON file.

Both contents have been processed (I'm iterating over the data):

Dictionary of YAML

[{'SynID': 66, 'action': 'userInit', 'appLocation': 0},
 {'SynID': 66, 'action': 'isEngager', 'appLocation': 0}]

Dictionary of JSON

{u'name': u'SynID', u'value': u'66'}
...
{u'name': u'action', u'value': u'isEngager'}

Finally, when doing the comparision, anything involving an integer (non string) fails. For example:

    if uriDict['name'] in expectedResultsSubDict and uriDict['value'] == expectedResultsSubDict[uriDict['name']]:
                Logger().l("[%s] Match: %s:%s == %s" % (pos, uriDict['name'], uriDict['value'], expectedResultsSubDict[uriDict['name']]))
    elif uriDict['name'] in expectedResultsSubDict:
         Logger().l("[%s] Name: %s, value: %s" % (pos, uriDict['name'], uriDict['value']))

The first if statement fails to match the key/value of SynID:66, but will match the second entry, action:isEngager.

2013-01-28 12:48:42,151 : DEBUG : [0] Name: SynID, value: 66
2013-01-28 12:48:42,151 : DEBUG : [0] Match: action:userInit == userInit

As you can see in the output, it doesn't match the SynID, but matches the action.

If I add in this line:

elif uriDict['name'] in expectedResultsSubDict and int(uriDict['value']) == int(expectedResultsSubDict[uriDict['name']]):
    Logger().l("[%s] Int Match: %s:%s == %s" % (pos, uriDict['name'], uriDict['value'], expectedResultsSubDict[uriDict['name']]))

It will work properly on the SynID (and other field that have ints) but fail on the string fields. I also tried changing the '==' comparison to 'is', and in that case, nothing matched.

Q) How do I compare the values of my dictionary?

2 Answers 2

2

You're comparing 66 to '66', which are not equal - one is an int and one is a string. Python does no implicit conversions between types, owing to its philosophy of 'explicit is better than implicit' . To get it to work, I'd convert both to string first and then compare, like so

if uriDict['name'] in expectedResultsSubDict and str(uriDict['value']) == str(expectedResultsSubDict[uriDict['name']]):
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that works. I have to convert all items because I don't know which are ints and which are not.
0

There is no problem with unicode strings here: you're comparing a string to integer, and getting False because, um, one of them is string and another one integer.

You can convert a value to integer with int(my_value_which_is_string_or_number).

2 Comments

Yes, I get that. I even stated that's the problem. However, I don't know which field the is an int ... so how can I convert to an int, if I don't the field is suppose to be an int ... ? What I'm asking is more of a pattern question - what pattern would you apply to solve this solution. I don't have this problem in other languages.
Another answer about converting anything to string is better for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.