Ere*_*rez 3 python django python-2.6 django-queryset
在我目前的django项目中,我有一个存储非常长字符串的模型(每个数据库条目可以是5000-10000甚至更多字符)然后我需要在用户调用记录时拆分它们(它确实需要在一个记录在数据库中).我需要的是返回一个列表(queryset?取决于是否在"SQL"部分或获取所有列表并在视图中进行解析)更短的字符串(列表中每个sting 100到500个字符我返回到模板).
我无法在任何地方找到python split命令,也没有示例或任何类型的答案....
我可以随时计算单词并附加但计算单词....但我确信必须有某种功能来处理这类事情....
编辑:谢谢大家,但我想我不明白,
例:
字符串:"这是一个非常长的字符串,有很多很多很多句子,没有一个字符我可以用来分割,只需按字数"
该字符串是django模型的textField.
我需要分开它,让我说每5个字,所以我会得到:
['这是一个非常长的字符串','有许多很多','还有更多的句子和','没有一个字符','我可以用','分开,只是数字' ,'的话']
问题是,几乎每种编程语言都有每个单词的分割"实用函数的种类,但我在python中找不到一个.
谢谢,埃雷兹
>>> s = "This is a very long string with many many many many and many more sentences and there is not one character that i can use to split by, just by number of words"
>>> l = s.split()
>>> n = 5
>>> [' '.join(l[x:x+n]) for x in xrange(0, len(l), n)]
['This is a very long',
'string with many many many',
'many and many more sentences',
'and there is not one',
'character that i can use',
'to split by, just by',
'number of words']
Run Code Online (Sandbox Code Playgroud)