How to change delimiter in list python

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']


How do you change the delimiter in Python?

How to change the delimiter in a CSV file.
Create a new Python file in the location where your CSV file is saved. ... .
Open up an Anaconda Prompt instance. ... .
Type python change_delimiter.py (replacing change_delimiter.py with the name of your Python file) then press Enter..

How do you replace a delimiter?

Answer:.
In Microsoft Windows, click the Start button, and then click Control Panel..
Open the dialog box for changing Regional and Language settings..
In the dialog box, look for the List separator setting. (Location may vary based on Windows version. ... .
Enter the desired list separator..
Click Apply and then click OK..

How do you replace commas in a list in Python?

Using the replace() function to replace comma with space in list in Python. In Python, we can replace substrings within a string using the replace() function. Using this function, we can replace comma with space in list in Python. It returns a new string with the substituted substring.

How do I remove delimiter from a list in Python?

You could try something like this:.
Given delimiters d , join them to a regular expression. ... .
Split the string using this regex with re.split >>> s = 'hey-you...are you ok?' >>> ... .
Join all the non-empty fragments back together >>> ' '.join(w for w in re.split("["+"\\".join(d)+"]", s) if w) 'hey you are you ok'.