Python 2如何将values_list操作返回的unicode列表更改为字符串列表

时间:2022-02-15 22:27:15

I performed this operation to retrieve a queryset:

我执行此操作以检索查询集:

Name.objects.values_list('name', flat=True)

And it returns these results:

它返回以下结果:

[u'accelerate', u'acute', u'bear', u'big']

The results are all in unicode (u'). How do I remove them all so that I get the result:

结果都是unicode(u')。如何将它们全部删除以便得到结果:

['accelerate', 'acute', 'bear', 'big']

5 个解决方案

#1


14  

If you want to encode in utf8, you can simply do:

如果你想用utf8编码,你可以简单地做:

definitions_list = [definition.encode("utf8") for definition in definitions.objects.values_list('title', flat=True)]

#2


9  

You could call str on all the values (note that map is a bit lazy, list() added to immediately turn it back into an indexable object):

您可以在所有值上调用str(请注意,map有点懒,添加了list()以立即将其转换回可索引对象):

thingy = list(map(str, [u'accelerate', u'acute', u'bear', u'big']))

Or use a list comprehension:

或者使用列表理解:

[str(item) for item in [u'accelerate', u'acute', u'bear', u'big']]

In the end though, why would you require them to be str explicitly; added to a django template (like {{ value }}), the u's will disappear.

最后,为什么你要求他们明确表达;添加到django模板(如{{value}}),u将消失。

#3


2  

I think the short way is using json.dumps()

我认为简短的方法是使用json.dumps()

json.dumps(definitions.objects.values_list('title', flat=True))

so you will get the string result as json format like

所以你会得到json格式的字符串结果

'["accelerate", "acute", "bear", "big" ...]'

and if you want to change that into python variable, simply use eval as the function, so your code more like this

如果你想把它改成python变量,只需使用eval作为函数,所以你的代码更像这样

json_format_string = json.dumps(definitions.objects.values_list('title', flat=True))

my_list = eval(json_format_string)  # ['accelerate', 'acute', 'bear', 'big' ...]

#4


1  

in python 2.7 justing using map without list is fine as well

在python 2.7中使用没有列表的地图也很好

list_of_python_strings = map(str, [u'accelerate'])

#5


0  

i used this

我用过这个

my_list = list(map(str, [u'accelerate', u'acute', u'bear', u'big'...]))

#1


14  

If you want to encode in utf8, you can simply do:

如果你想用utf8编码,你可以简单地做:

definitions_list = [definition.encode("utf8") for definition in definitions.objects.values_list('title', flat=True)]

#2


9  

You could call str on all the values (note that map is a bit lazy, list() added to immediately turn it back into an indexable object):

您可以在所有值上调用str(请注意,map有点懒,添加了list()以立即将其转换回可索引对象):

thingy = list(map(str, [u'accelerate', u'acute', u'bear', u'big']))

Or use a list comprehension:

或者使用列表理解:

[str(item) for item in [u'accelerate', u'acute', u'bear', u'big']]

In the end though, why would you require them to be str explicitly; added to a django template (like {{ value }}), the u's will disappear.

最后,为什么你要求他们明确表达;添加到django模板(如{{value}}),u将消失。

#3


2  

I think the short way is using json.dumps()

我认为简短的方法是使用json.dumps()

json.dumps(definitions.objects.values_list('title', flat=True))

so you will get the string result as json format like

所以你会得到json格式的字符串结果

'["accelerate", "acute", "bear", "big" ...]'

and if you want to change that into python variable, simply use eval as the function, so your code more like this

如果你想把它改成python变量,只需使用eval作为函数,所以你的代码更像这样

json_format_string = json.dumps(definitions.objects.values_list('title', flat=True))

my_list = eval(json_format_string)  # ['accelerate', 'acute', 'bear', 'big' ...]

#4


1  

in python 2.7 justing using map without list is fine as well

在python 2.7中使用没有列表的地图也很好

list_of_python_strings = map(str, [u'accelerate'])

#5


0  

i used this

我用过这个

my_list = list(map(str, [u'accelerate', u'acute', u'bear', u'big'...]))