如何在python中将unicode url列表转换为字符串

时间:2017-11-21 15:58:22

标签: python python-2.7 unicode

我需要将一个unicoded url列表转换为python中的字符串列表。我尝试过如下:

list_of_images = [u'http://cdn.fjfjfjfjfj.com/DesiyaImages/Image/2/nxd/mav/qyi/ebn/HA.jpg', u'http://cdn.njjk.com/DesiyaImages/Image/2/nxd/mav/qyi/ebn/HO.jpg', u'http://cdn.k.com/DesiyaImages/Image/2/nxd/mav/qyi/ebn/0000055334RD.jpg', u'http://cdn.m.com/DesiyaImages/Image/2/nxd/mav/qyi/ebn/0000055336RD.jpg', u'http://cdn.shf.com/DesiyaImages/Image/2/nxd/mav/qyi/ebn/0000055337RD.jpg', u'http://cdn.kjfdjhf.com/DesiyaImages/Image/2/nxd/mav/qyi/ebn/XT1.jpg', u'http://cdn.kjdfhkjsh.com/DesiyaImages/Image/2/nxd/mav/qyi/ebn/XT2.jpg', u'http://cdn.kskfisjkd.com/DesiyaImages/Image/2/nxd/mav/qyi/ebn/XT3.jpg', u'http://cdn.djfhsjfd.com/DesiyaImages/Image/2/nxd/mav/qyi/ebn/XT4.jpg', u'http://cdn.jsjfhdf.com/DesiyaImages/Image/2/nxd/mav/qyi/ebn/XT5.jpg', u'http://cdn.dfgjdhf.com/DesiyaImages/Image/2/nxd/mav/qyi/ebn/XT6.jpg']

converted_list = [i.encode('ascii','ignore').strip() for i in list_of_images]

它将每个char转换为单独的字符串,我需要删除它。我是python的新手。请帮我解决这个问题?

2 个答案:

答案 0 :(得分:0)

使用str()方法在Python中转换为字符串。 您可以执行以下操作:

converted_list = [str(image) for image in list_of_images]

答案 1 :(得分:0)

试试这个

    >>> list_of_images = [u'http://cdn.fjfjfjfjfj.com/DesiyaImages/Image/2/nxd/mav/qyi/ebn/HA.jpg', u'http://cdn.njjk.com/D
esiyaImages/Image/2/nxd/mav/qyi/ebn/HO.jpg', u'http://cdn.k.com/DesiyaImages/Image/2/nxd/mav/qyi/ebn/0000055334RD.jpg',
 u'http://cdn.m.com/DesiyaImages/Image/2/nxd/mav/qyi/ebn/0000055336RD.jpg', u'http://cdn.shf.com/DesiyaImages/Image/2/n
xd/mav/qyi/ebn/0000055337RD.jpg', u'http://cdn.kjfdjhf.com/DesiyaImages/Image/2/nxd/mav/qyi/ebn/XT1.jpg', u'http://cdn.
kjdfhkjsh.com/DesiyaImages/Image/2/nxd/mav/qyi/ebn/XT2.jpg', u'http://cdn.kskfisjkd.com/DesiyaImages/Image/2/nxd/mav/qy
i/ebn/XT3.jpg', u'http://cdn.djfhsjfd.com/DesiyaImages/Image/2/nxd/mav/qyi/ebn/XT4.jpg', u'http://cdn.jsjfhdf.com/Desiy
aImages/Image/2/nxd/mav/qyi/ebn/XT5.jpg', u'http://cdn.dfgjdhf.com/DesiyaImages/Image/2/nxd/mav/qyi/ebn/XT6.jpg']      
>>> newlist=[]                                                                                                         
>>> for i in range(len(list_of_images)-1):                                                                             
...  newlist.append(str(list_of_images[i]))                                                                            
...                                                                                                                    
>>> newlist                                                                                                            
['http://cdn.fjfjfjfjfj.com/DesiyaImages/Image/2/nxd/mav/qyi/ebn/HA.jpg', 'http://cdn.njjk.com/DesiyaImages/Image/2/nxd
/mav/qyi/ebn/HO.jpg', 'http://cdn.k.com/DesiyaImages/Image/2/nxd/mav/qyi/ebn/0000055334RD.jpg', 'http://cdn.m.com/Desiy
aImages/Image/2/nxd/mav/qyi/ebn/0000055336RD.jpg', 'http://cdn.shf.com/DesiyaImages/Image/2/nxd/mav/qyi/ebn/0000055337R
D.jpg', 'http://cdn.kjfdjhf.com/DesiyaImages/Image/2/nxd/mav/qyi/ebn/XT1.jpg', 'http://cdn.kjdfhkjsh.com/DesiyaImages/I
mage/2/nxd/mav/qyi/ebn/XT2.jpg', 'http://cdn.kskfisjkd.com/DesiyaImages/Image/2/nxd/mav/qyi/ebn/XT3.jpg', 'http://cdn.d
jfhsjfd.com/DesiyaImages/Image/2/nxd/mav/qyi/ebn/XT4.jpg', 'http://cdn.jsjfhdf.com/DesiyaImages/Image/2/nxd/mav/qyi/ebn
/XT5.jpg']                                                                                                             
>>>