I have a list
[u'Scanned by CamS\nINCOME TAN DEPARTMENT\nINDER SINGH\nAMAR JEETSINGH FIORA\n2407/1985\nMimi\nAAPPF5793,\nGOVT of INDIA\n']
is there a way to change the list separator from '\n'
to ','
using python?
My expected output is
['Scanned by CamS','INCOME TAN DEPARTMENT','INDER SINGH','AMAR JEETSINGH FIORA','2407/1985','Mimi','AAPPF5793,','GOVT of INDIA']
This is fake PAN.
asked Feb 28, 2017 at 5:34
Dipanwita DasDipanwita Das
1992 gold badges6 silver badges14 bronze badges
7
using split
on
first list item [desired string] :
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', '']
last element is empty because of last '\n'
answered Feb 28, 2017 at 5:38
ᴀʀᴍᴀɴᴀʀᴍᴀɴ
4,3118 gold badges33 silver badges54 bronze badges
You can try this both..
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 ,
answered Feb 28, 2017 at 5:48
Shivkumar kondiShivkumar kondi
6,0788 gold badges29 silver badges56 bronze badges
2
Given List of Strings and replacing delimiter, replace current delimiter in each string.
Input : test_list = [“a, t”, “g, f, g”, “w, e”, “d, o”], repl_delim = ‘ ‘
Output : [“a t”, “g f g”, “w e”, “d o”]
Explanation : comma is replaced by empty spaces at each string.Input : test_list = [“g#f#g”], repl_delim = ‘, ‘
Output : [“g, f, g”]
Explanation : hash is replaced by comma at each string.
Method #1 : Using replace[]
+ loop
The combination of above functions provide a brute force method to solve this problem. In this, a loop is used to iterate through each string and perform replacement using replace[].
test_list
=
[
"a, t"
,
"g, f, g"
,
"w, e"
,
"d, o"
]
print
[
"The original list is : "
+
str
[test_list]]
repl_delim
=
'#'
res
=
[]
for
ele
in
test_list:
res.append[ele.replace[
", "
, repl_delim]]
print
[
"Replaced List : "
+
str
[res]]
Output :
The original list is : ['a, t', 'g, f, g', 'w, e', 'd, o'] Replaced List : ['a#t', 'g#f#g', 'w#e', 'd#o']
Method #2 : Using list comprehension + replace[]
The combination of above functions can provide one liner to this problem. This is similar to above method, just encapsulated in list comprehension.
test_list
=
[
"a, t"
,
"g, f, g"
,
"w, e"
,
"d, o"
]
print
[
"The original list is : "
+
str
[test_list]]
repl_delim
=
'#'
res
=
[sub.replace[
', '
, repl_delim]
for
sub
in
test_list]
print
[
"Replaced List : "
+
str
[res]]
Output :
The original list is : ['a, t', 'g, f, g', 'w, e', 'd, o'] Replaced List : ['a#t', 'g#f#g', 'w#e', 'd#o']