【问题标题】:How to use explode in django?如何在 django 中使用爆炸?
【发布时间】:2013-01-18 06:03:28
【问题描述】:

嗨,我在 django 中有一个变量,其中包含一个 value 类似于 ((u'rahul',),(u'sdfsd',)( u'fsdfdsf',)) 我想 explode *u* 从每个..... 我该怎么做?

【问题讨论】:

  • 那个值是 Queryset 的形式吗?
  • 我认为您正在寻找的是带有 flat=True 参数的 values_list。

标签: python django django-templates mezzanine


【解决方案1】:
tuple(tuple(__.encode() for __ in _) for  _ in f)

((u'rahul'), (u'sdfsd'), (u'fsdfdsf'))这样的元组

将导致(('rahul',), ('sdfsd',), ('fsdfdsf',))

encode 可以接受不同的编码,如“utf-8”、“ascii”。默认编码为utf-8。处理错误的默认方法是引发UnicodeEncodeError。您可以将“忽略”指定为ignore 错误。

encode 的帮助显示如下:

编码(...) S.encode([encoding[,errors]]) -> 字符串或unicode

Encodes S using the codec registered for encoding. encoding defaults
to the default encoding. errors may be given to set a different error
handling scheme. Default is 'strict' meaning that encoding errors raise
a UnicodeEncodeError. ...

【讨论】:

【解决方案2】:

U 表示 Unicode,如果要删除 u

试试这个

>>>(u'rahul').encode('utf8')
>>> rahul

【讨论】:

  • 这实际上不起作用我想从所有值中动态删除 u ((u'rahul',),(u'sdfsd',)(u'fsdfdsf',))
  • 在 html 中显示数据的位置
  • 那么您需要将这些结果作为 JSON 发送,请参阅 this linkthis also useful
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-21
  • 2021-09-20
  • 1970-01-01
  • 1970-01-01
  • 2014-12-15
相关资源
最近更新 更多