-2

I have read a text file and inserted data to Riak DB using python.

csv_data = csv.reader(file('abc.txt')) 

txt file contains two field separated with @@

tempstring = str(row).split('@@')

and inserted in riak DB

new_user = user_buckets.new(FirstUser,data={
                       'user':FirstUser,
                       'friendlist':FriendList,
                       'is_active':True
                        })
            new_user.store()

to fetch the data from Riak DB i use Map

user_buckets.map("""
            function(v) { 
                var data = JSON.parse(v.values[0].data);
                if(data.is_active == true) { 
                    return [[v.key, data]]; 
                } 
                return []; 
            }
         """)

        for result in user_buckets.run():
            print "%s - %s" % (result[0], result[1])

and it gives in the format

Dinesh - {u'friendlist': [u'Bakshi', u' Kishna'], u'is_active': True, u'user': u'Dinesh'}

Here how can I remove u in the output format? Does converting to string while inserting to Riak cause to form u?

Edit 1

I fetched Riak DB using mapreduce

function(v) {
var data = JSON.parse(v.values[0].data);
var mapped = [];
var grouping = [];
if(data.is_active == true) {
       userValue = data.user;
       friendListValue = data.friendlist;
       for (var i=0; i<friendListValue.length; i++){
             friendValue = friendListValue[i];
             mapped.push([userValue, friendValue, friendListValue]);
             grouping.push([mapped[i]]);
        }
}
}

Here grouping is multidimensional array which contains data like

Example

[u'Dinesh', u'Shiva', [u'Bakshi', u' Kishna', u' Hanks', u' Shiva', u' Bindu', u' Hari', u' Karma', u' Sita']]
[u'Shiva', u'Dinesh', [u'Hanks', u' Tom', u' Karma', u' Hari', u' Dinesh']]

and when i am comparing the values in array using

if(grouping[j][0]==grouping[k][1]&& grouping[j][1]==grouping[k][0])

In the above given like example case, it should return true but it gives error. So i think it is due to u. Now how can i compare the array and find out the match case? here i think i made a mistake in if condition.

bikas
  • 81
  • 1
  • 8
  • The `u` just means unicode, it doesn't actually exist; it's just a visual marker when you print the data. However, `Dinesh` is not JSON but a Python dictionary. – roganjosh Sep 20 '16 at 09:24
  • Data fetched from Riak is formatted as in example above. i want to compare the values (comparing condition as above) then it gives the error. For the example like above it should return true but couldnot get it. – bikas Sep 20 '16 at 12:10
  • Have you followed the link that was marked as a duplicate and read the answer by Daniel Roseman? There's a difference between `True` and "error". What is the error? – roganjosh Sep 20 '16 at 12:20

1 Answers1

2

You don't have JSON data, you have the string representation of a Python dictionary. You need to explicitly convert it to JSON with json.dumps().

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • I am using python to read the text file and to insert in Riak, Here i have fethced data using javascript and added to multidimensional array grouping. Now i want to check the condition as above so the it returns true for the case as given in example above. So to get this result where can i use json.dumps(). – bikas Sep 20 '16 at 12:20