做下笔记, Django 中的一些文本处理工具. 觉得挺有用的:)

get_text_list(items, last_word='or')

from django.utils.text import get_text_list
print 'You can use Python %s' % get_text_list([1, 2, 3])
#output 
'You can use Python 1, 2 or 3'

print get_text_list(['me', 'myself', 'I'], 'and')
#output 
'me, myself and I'

truncate_words(s, num)

from django.utils.text import truncate_words
print truncate_words('This is short, but you get the idea.', 4)
#output
'This is short, but ...'

truncate_html_words(s, num)

from django.utils.text import truncate_html_words
print truncate_html_words('It just does the right thing.', 4)
#output
'It just does the ...<\q>'

wrap(text, width)

from django.utils.text import wrap
text = """
This is a long section of text, destined to be broken apart.
It is only a test.
"""
print wrap(text, 35)
#output
'This is a long section of text,
destined to be broken apart.
It is only test.'