1

In one of my forms i send two bits of information like this:

  <input type ="hidden" name= "items" value="{{item.pk}} {{item.name}}">

when I get the information with request.POST.get, I get "202 book".

How can I seperate just the number from the string or just the word? I did a split:

  pks = request.POST.getlist("items")
  for pk in pks:
      pk.split(' ',1)

but the number isn't always a 1 or 2 digit number, it may be 2 or 3 etc. Also I might have the situations where the name is book1 so I would need that last "1" to remain.

Any ideas how I may go about this?

3
  • if there is always space between number and text part of the string, then why don't you split by space? You could also use regexp... Commented Jun 28, 2014 at 20:01
  • For 202 book, book1 or something very similar you can use regex and \d+ Commented Jun 28, 2014 at 20:05
  • I tried split by space but its not working for me, this is what I tried for pk in pks: pk.split(' '), is that the wrong syntax? I will look up regex Commented Jun 28, 2014 at 20:26

4 Answers 4

2

Unless I'm misinterpreting your question, why don't you just split the string and extract the first and second element from the resulting list? Demo:

>>> mystr = "202 book"
>>> lst = "202 book".split()
>>> num = lst[0] # or int(lst[0])
>>> num
'202'
>>> other = lst[1]
>>> other
'book'

If you have a string which contains numbers and words which could contain a number, but should not count when looking for numbers, you can do:

>>> mystr = 'This 101 5is 4 600a 42 de3mo string12'
>>> re.findall(r'\b\d+\b', mystr)
['101', '4', '42']
>>> re.findall(r'(?!\d+\b)\w+', mystr)
['This', '5is', '600a', 'de3mo', 'string12']
Sign up to request clarification or add additional context in comments.

2 Comments

I read again description and I think SO needs only split() to get expected result
I saw the error in my ways with your suggestion and this also worked for me as well.
1

you can use this surely it work !

my_digit = ''.join(temp for temp in my_string if temp.isdigit())

or if you want to have the value of digit you can use int(my_digit)

2 Comments

I don't fully understand what the code is doing as I'm not familiar with join, but it worked well for me, thank you!
im glad ! The Python join() method is a string method, and takes a list and join them as string !
1

You have some useful answers on how to split up the data, but since you seem to be controlling both the template and the view, why are you putting them in the same field to begin with? Why not put them in two separate fields?

<input type="hidden" name="item_pk" value="{{item.pk}}">
<input type="hidden" name="item_name" value="{{item.name}}">

3 Comments

This is because I have the values coming from a form, and the input is through a checkbox, I shouldve been more clear on that. If know of a way to send them both at the same time from each of the selected checkboxes, I'd go with that
Sounds like an XY problem, if you ask me. You should ask what you really want to know (in a new question): show the form and checkboxes, and ask how to get the correct data out.
The answers here helped me, so I'll save that for another issue. Thank you :)
0

I have never used django, but how about using regular expressions?

import re

pk = "202 book";
print re.findall(r'\d\d?\d?',pk)

Comments

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.