如何搜索单词'查看'在' u'比如它是u'myprofile.view_department'
然后从这个长字符串/段落中得到myprofile, view, department
- Python新手 -
set([u'auth.view_group', u'auth.add_user', u'transaction.change_sender', u'myprofile.view_department', u'auth.add_permission', u'transaction.add_receiver', u'myprofile.delete_routineusers', u'transaction.change_receiver', u'sessions.change_session', u'transaction.delete_receiver', u'registration.change_registrationprofile', u'auth.view_user', u'auth.delete_permission', u'transaction.view_sender', u'myprofile.change_hrworker', u'registration.add_registrationprofile', u'admin.view_logentry', u'registration.view_registrationprofile', u'contenttypes.change_contenttype', u'myprofile.view_routineusers', u'sessions.delete_session', u'myprofile.change_department', u'myprofile.delete_hrworker', u'admin.change_logentry', u'myprofile.change_allusers', u'admin.delete_logentry', u'transaction.delete_sender', u'auth.add_group', u'myprofile.delete_department', u'myprofile.add_department', u'contenttypes.delete_contenttype', u'auth.view_permission', u'myprofile.add_allusers', u'myprofile.delete_allusers', u'auth.change_permission', u'transaction.view_receiver', u'auth.change_group', u'registration.delete_registrationprofile', u'contenttypes.view_contenttype', u'sessions.add_session', u'auth.delete_group', u'auth.delete_user', u'myprofile.view_hrworker', u'transaction.add_sender', u'myprofile.add_hrworker', u'admin.add_logentry', u'sessions.view_session', u'auth.change_user', u'myprofile.add_routineusers', u'contenttypes.add_contenttype', u'myprofile.view_allusers', u'myprofile.change_routineusers'])
我尝试了什么:
l = request.user.get_all_permissions().split()
if any(word in all_text for word in l):
print 'found one of em'
l = request.user.get_all_permissions().split()
l11 = [elem.strip().split(',') for elem in l]
l11 = []
for elem in l:
l11.extend(elem.strip().split(','))
print l1
这样的代码会生成属性错误'set' object has no attribute 'split'
我知道这指向set([
但是如何从中获取几个字?
答案 0 :(得分:1)
试试这个:
>>> str = "set([u'auth.view_group', u'auth.add_user', u'transaction.change_sender', u'myprofile.view_department', u'auth.add_permission', u'transaction.add_receiver', u'myprofile.delete_routineusers', u'transaction.change_receiver', u'sessions.change_session', u'transaction.delete_receiver', u'registration.change_registrationprofile', u'auth.view_user', u'auth.delete_permission', u'transaction.view_sender', u'myprofile.change_hrworker', u'registration.add_registrationprofile', u'admin.view_logentry', u'registration.view_registrationprofile', u'contenttypes.change_contenttype', u'myprofile.view_routineusers', u'sessions.delete_session', u'myprofile.change_department', u'myprofile.delete_hrworker', u'admin.change_logentry', u'myprofile.change_allusers', u'admin.delete_logentry', u'transaction.delete_sender', u'auth.add_group', u'myprofile.delete_department', u'myprofile.add_department', u'contenttypes.delete_contenttype', u'auth.view_permission', u'myprofile.add_allusers', u'myprofile.delete_allusers', u'auth.change_permission', u'transaction.view_receiver', u'auth.change_group', u'registration.delete_registrationprofile', u'contenttypes.view_contenttype', u'sessions.add_session', u'auth.delete_group', u'auth.delete_user', u'myprofile.view_hrworker', u'transaction.add_sender', u'myprofile.add_hrworker', u'admin.add_logentry', u'sessions.view_session', u'auth.change_user', u'myprofile.add_routineusers', u'contenttypes.add_contenttype', u'myprofile.view_allusers', u'myprofile.change_routineusers'])"
>>> import re
>>> pat = re.compile('(?<=u\')([a-z]+).([a-z]+).([a-z]+)')
>>> pat.findall(str)
[('auth', 'view', 'group'), ('auth', 'add', 'user'), ('transaction', 'change', 'sender'), ('myprofile', 'view', 'department'), ('auth', 'add', 'permission'), ('transaction', 'add', 'receiver'), ('myprofile', 'delete', 'routineusers'), ('transaction', 'change', 'receiver'), ('sessions', 'change', 'session'), ('transaction', 'delete', 'receiver'), ('registration', 'change', 'registrationprofile'), ('auth', 'view', 'user'), ('auth', 'delete', 'permission'), ('transaction', 'view', 'sender'), ('myprofile', 'change', 'hrworker'), ('registration', 'add', 'registrationprofile'), ('admin', 'view', 'logentry'), ('registration', 'view', 'registrationprofile'), ('contenttypes', 'change', 'contenttype'), ('myprofile', 'view', 'routineusers'), ('sessions', 'delete', 'session'), ('myprofile', 'change', 'department'), ('myprofile', 'delete', 'hrworker'), ('admin', 'change', 'logentry'), ('myprofile', 'change', 'allusers'), ('admin', 'delete', 'logentry'), ('transaction', 'delete', 'sender'), ('auth', 'add', 'group'), ('myprofile', 'delete', 'department'), ('myprofile', 'add', 'department'), ('contenttypes', 'delete', 'contenttype'), ('auth', 'view', 'permission'), ('myprofile', 'add', 'allusers'), ('myprofile', 'delete', 'allusers'), ('auth', 'change', 'permission'), ('transaction', 'view', 'receiver'), ('auth', 'change', 'group'), ('registration', 'delete', 'registrationprofile'), ('contenttypes', 'view', 'contenttype'), ('sessions', 'add', 'session'), ('auth', 'delete', 'group'), ('auth', 'delete', 'user'), ('myprofile', 'view', 'hrworker'), ('transaction', 'add', 'sender'), ('myprofile', 'add', 'hrworker'), ('admin', 'add', 'logentry'), ('sessions', 'view', 'session'), ('auth', 'change', 'user'), ('myprofile', 'add', 'routineusers'), ('contenttypes', 'add', 'contenttype'), ('myprofile', 'view', 'allusers'), ('myprofile', 'change', 'routineusers')]
答案 1 :(得分:0)
如果您使用split()
s = u'myprofile.view_department'
temp = s.split('.')
words = [temp[0]] + temp[1].split('_')
print words
# [u'myprofile', u'view', u'department']