1

I hate that I have to ask such a basic question, but I can't seem to get the split method to work in python.

I have a dictionary, when I print it, it looks like:

data_dict = <QueryDict: {u'ids': [u'1012,738'], u'ct': [u'9']}>

I want to create an array of id objects from the 'ids' list, as in:

ids = [1012, 738]

It appears that the list 'ids' is really just a string of ids separated by commas, so I tried:

id_string = data_dict['ids']
ids = id_string.split(',')
for id in ids:
    print(id)

Nothing prints. Then I thought, perhaps the u' means its not a string and I need to convert it to a string first to get split to work, so I tried:

id_string = data_dict['ids']
id_string = str(id_string)
ids = id_string.split(',')
for id in ids:
    print(id)

Nothing prints. Not sure what I am doing wrong.

0

2 Answers 2

2

data_dict['ids'] seems like it's actually a list which contains a string. So try id_string = data_dict['ids'][0].

A u'foo' is a Unicode string. It should still have all the normal string methods.

Also, if you're using Django and running the code with python manage.py runserver (and not running in production!), you can inject import pdb; pdb.set_trace() into your code. Then, when you load the page, the server output will contain a pdb prompt which you can use to experiment just like in the Python shell.

A small note if you do start using PDB in this manner: PDB has some one-letter commands and other things that will take precedence over Python code. You can use ! to make sure PDB runs your code like Python. E.g.:

(Pdb) list(range(3))
*** Error in argument: '(range(3))'

PDB has its own list command that you can use to show source code.

(Pdb) !list(range(3))
[0, 1, 2]

By using ! first, the command is properly treated like Python.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I just tried using [0], but it still doesn't print. When i print data_dict['ids'], i just get '1012,738', so I think its automatically just printing [0].
Yes, I've forgotten but that's probably a convenience feature of the Django QueryDict class: if an item is a one-element list it'll give you the item in the list instead of the list, or something like that. That's my guess anyway
1

[u'1012,738'] is a list so you've to use id_string = data_dict['ids'][0] to get the first element from that list.

To convert the items of that string into list of integers use either map or a list comprehension:

#map
map(int, data_dict['ids'][0].split(','))

#LC
[int(x) for x in data_dict['ids'][0].split(',')]

Demo:

>>> strs = '1012,738'
>>> map(int, strs.split(','))
[1012, 738]
>>> [int(x) for x in  strs.split(',')]
[1012, 738]

3 Comments

data_dict['ids'] returns [u'1012,738'], doesn't it?
thanks! for some reason, data_dict['ids'] returned the string, i didn't need [0].
@user1697845 glad that helped, looks like QueryDict behaves differently than normal dicts.

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.