Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从django.utils.safestring.SafeText中获取字符串_Python_Django_Python 2.7 - Fatal编程技术网

Python 如何从django.utils.safestring.SafeText中获取字符串

Python 如何从django.utils.safestring.SafeText中获取字符串,python,django,python-2.7,Python,Django,Python 2.7,我在单元测试中得到一个django.utils.safestring.SafeText: ipdb> mail.outbox[0].body u'Dear John Doe,<br>\n<p>\nYou have received a .. ipdb> type(mail.outbox[0].body) <class 'django.utils.safestring.SafeText'> ipdb>mail.outbox[0]。正文 亲爱的约翰

我在单元测试中得到一个django.utils.safestring.SafeText:

ipdb> mail.outbox[0].body
u'Dear John Doe,<br>\n<p>\nYou have received a ..

ipdb> type(mail.outbox[0].body)
<class 'django.utils.safestring.SafeText'>
ipdb>mail.outbox[0]。正文
亲爱的约翰·多伊,
\n\n您收到了一封。。 ipdb>类型(邮件发件箱[0]。正文)

我想将上面的内容转换成字符串,这样我就可以去掉
\n
字符。。ie我想使用
rstrip()
方法。。但我显然不能在
django.utils.safestring.SafeText
对象上这样做。想法?

基于SafeText创建新字符串

str(mail.outbox[0].body)

您可以使用
django.utils.safestring.SafeText
对象执行您想要执行的操作。您可以将几乎所有方法作为字符串应用于SafeText对象。可用的方法有

'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'islower', 'isnumeric', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill'
但它们将返回unicode对象。例如:-

>>> from django.utils.safestring import SafeText
>>> my_safe_text = SafeText('Dear John Doe,<br>\n<p>\nYou have received a ..  ')
>>> type(my_safe_text)
<class 'django.utils.safestring.SafeText'>
>>> my_replaced_unicode = my_safe_text.replace('\n','')
>>> my_replaced_unicode
u'Dear John Doe,<br><p>You have received a ..  '
>>> type(my_replaced_unicode)
<type 'unicode'>
>>> my_rstriped_unicode = my_safe_text.rstrip()
>>> my_rstriped_unicode
u'Dear John Doe,<br>\n<p>\nYou have received a ..'
>>> type(my_rstriped_unicode)
<type 'unicode'>
>>从django.utils.safestring导入SafeText
>>>my_-safe\u text=SafeText(‘亲爱的约翰·多伊,
\n\n您收到了一封……’) >>>键入(我的安全文本) >>>my_replaced_unicode=my_safe_text.replace('\n','') >>>我的密码替换了unicode密码 亲爱的约翰·多伊,
你收到了 >>>类型(我的\u替换\u unicode) >>>my_rstriped_unicode=my_safe_text.rstrip() >>>我的车撞到了unicode 亲爱的约翰·多伊,
\n\n您收到了一份 >>>类型(my_rstriped_unicode)
从Django 2.0.4开始,
str(mail.outbox[0].body)
不再工作——它返回相同的SafeText对象。(原因请参见。)

相反,您可以添加空字符串以恢复常规字符串:

type(mail.outbox[0].body+“”)=str


为了帮助其他人,我遇到了这个问题,因为我试图将SafeText从Django传递到
pathlib.Path
,这会抛出
TypeError:can't intern SafeText
。添加一个空字符串可以修复它。

通过
django.utils.encoding.force_text
传递它?force_text不起作用这在python命令行上起作用。。但它在实际代码中不起作用