如何在python中更改列表分隔符?

时间:2017-02-28 05:34:36

标签: python

我有一个清单

[u'Scanned by CamS\nINCOME TAN DEPARTMENT\nINDER SINGH\nAMAR JEETSINGH FIORA\n2407/1985\nMimi\nAAPPF5793,\nGOVT of INDIA\n']

有没有办法使用python将列表分隔符从'\n'更改为','

我的预期输出是

['Scanned by CamS','INCOME TAN DEPARTMENT','INDER SINGH','AMAR JEETSINGH FIORA','2407/1985','Mimi','AAPPF5793,','GOVT of INDIA']

这是假PAN。

2 个答案:

答案 0 :(得分:2)

在第一个列表项(所需字符串)上使用split

l = [u'Scanned by CamS\nINCOME TAN DEPARTMENT\nINDER SINGH\nAMAR JEETSINGH FIORA\n2407/1985\nMimi\nAAPPF5793,\nGOVT of INDIA\n']
l =  l[0].split('\n')
l # ['Scanned by CamS', 'INCOME TAN DEPARTMENT', 'INDER SINGH', 'AMAR JEETSINGH FIORA', '2407/1985', 'Mimi', 'AAPPF5793,', 'GOVT of INDIA', '']
由于上一个'\n'

最后一个元素为空

答案 1 :(得分:-1)

你可以尝试这两个..

a = [u'Scanned by CamS\nINCOME TAN DEPARTMENT\nINDER SINGH\nAMAR JEETSINGH FIORA\n2407/1985\nMimi\nAAPPF5793,\nGOVT of INDIA\n']

print str(a[0]).split('\n')
#['Scanned by CamS', 'INCOME TAN DEPARTMENT', 'INDER SINGH', 'AMAR JEETSINGH FIORA', '2407/1985', 'Mimi', 'AAPPF5793,', 'GOVT of INDIA', '']

print str(a[0]).replace('\n',' ,')
#Scanned by CamS ,INCOME TAN DEPARTMENT ,INDER SINGH ,AMAR JEETSINGH FIORA ,2407/1985 ,Mimi ,AAPPF5793, ,GOVT of INDIA ,