how to fetch only content from table by avoiding unwanted codes [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I was trying to fetch text content from table which works well but along with result it print unwanted codes
my code is here
searchitem = searchme.objects.filter(face = after) .values_list ("tale" , flat = True)
the contents are text
the result I receive is "querySet Prabhakaran seachitem"
but I only want o get result "Prabhakaran"
model is this
class searchme ( models.Model):
face = models.TextField()
tale = models.TextField ()

From the official django documentation :
A common need is to get a specific field value of a certain model instance. To achieve that, use values_list() followed by a get() call:
So use:
searchme.objects.values_list('tale', flat=True).get(face=after)

Related

python soup response parsing header and value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed yesterday.
Improve this question
i have soup response text with multiple group and sub groups.
i want to get automatic all groups and their values .
how can i do it ?
In the end, I want to get the title and the value for each group. The best thing for me is for each group to have its values separately.
OrderedDict([('#id',
'boic'),
('mc:id',
'boic'),
('mc:ocb-conditions',
OrderedDict([('mc:rule-deactivated',
'true'),
('mc:international',
'true')])),
('mc:cb-actions',
OrderedDict([('mc:allow',
'false')]))])
My goal is to get to a state where I get the following output:
'#id','boic'
'mc:id','boic'
'mc:ocb-conditions'
'mc:rule-deactivated','true'
'mc:international', 'true'
'mc:cb-actions'
'mc:allow','false'
i try to use
' '.join(BeautifulSoup(soup_response, "html.parser").findAll(text=True))
and got all values But I'm missing the titles of the values.

get part of an email follow a pattern [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 months ago.
Improve this question
Input: abc#xyz.com
Output: xyz
my code is below
email = "123#helloworld.com"
new_email = email.split('#')[1]
domain = new_email.split('.')[0]
Please help me with other methods to get the part of it?
UPDATE (Based on a comment, see below)
If you have input like 123#helloworld.python.com or 123#helloworld.yahoo. And you want to extract only helloworld. You can use
result = re.search(r'#([^\.]+)\.', email).group(1)
BEFORE UPDATE
You can use re module.
import re
email = email = '123#helloworld.com'
result = re.search(r'#(.+)\.com$', email)
if result:
result = result.group(1)
print(result)
Output:
helloworld
If you are sure that you will always have something in between # and .com. You can remove if statement.
import re
email = email = '123#helloworld.com'
result = re.search(r'#(.+)\.com$', email).group(1)
print(result)

I want to print names of employees who have both work number and mobile number.below is my json body [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to print firstname of employees who have both work number and mobile number. Below is my json body. I am facing difficulty in getting inside phoneNumbers attribute. My final output should be: "Adhi as Adhi has both work and mobile numbers".
I am not able to iterate the inner dictionary of phoneNumbers attribute.Can you please help me on this.
This is my python code
for i in Data['users']:
for j in i['phoneNumbers']:
for i in range(len(j)):
if j['type']=="work" and j['type']=="mobile":
print("Firstname",i['firstName'])
You can loop over the users and check if the work and mobile number are present:
for user in Data['users']:
has_mobile_number = False
has_work_number = False
for phonenumber in user['phoneNumbers']:
if phonenumber['type'] == 'work':
has_work_number = True
if phonenumber['type'] == 'mobile':
has_mobile_number = True
if has_work_number and has_mobile_number:
print('Firstname', user['firstName'])
Also, I recommend not using i and j when not talking about indexes. In you code, i is a dict representing a user and j is a dict representing a phone. I replaced them with user and phonenumber for more clarity in the code above.

Django limiting query [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a problem with django pagination. In my table I have 13,618 records but do pagination, I do not return results.
>>> from api.models import Post
>>> posts = Post.objects.all()
>>> posts.count()
13618
>>> posts = Post.objects.all()[10:10]
>>> posts.count()
0
The problem is in your slicing:
posts = Post.objects.all()[10:10]
You're asking for the 10th item to the 9th (10-1) item, which is an empty list. The same would happen if you did this:
ls = [1,2,3]
ls[1:1] # => []
It looks like you want 10 items starting from the 10th, in which case you should do:
posts = Post.objects.all()[10:20]

Multiple fields in the where clause of a QuerySet? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Mutliple fields in the where clause of a QuerySet
qs_new = model_obj.objects.all()
You can use filter()to define a WHERE clause for your query:
qs_new = model_obj.objects.filter(...)
The QuerySet documentation describes various ways in which you can combine conditions and filters.
qs_new = model_obj.objects.filter(col_name='value', col_name2='value2')
filters_con = { 'a': 1, 'b': 2, 'c': 3 }
model_name.objects.filter(**filters_con)
it is working fine for me.

Categories