'How would I use .split() to split a string by \n instead of .split() reading \n as a new line

I'm trying to split up a string inside of a list into a list, and the string contains \n as a character. Whenever I use .split("\n"), its of course splitting the string at a new line, but I need it to split at the character \n. Is there anyway to make .split() interpret it this way?

Example:

#original list
['\nSK Telecom Co. Ltd. ADR\nSKM\n12/31/2021\n1.49\nN/A\nN/A']

what id like to create with .split() function:

[ [SK Telecom CO. Ltd. ADR] , [SKM] , [12/31/2021] , [1.49], [N/A], [N/A] ]

if you have any idea on how to help i'd be very grateful, thanks!



Solution 1:[1]

what about:

a= ['\nSK Telecom Co. Ltd. ADR\nSKM\n12/31/2021\n1.49\nN/A\nN/A']
print(a[0].strip('\n').split('\n'))

Solution 2:[2]

If I interpret your question literally, it looks like you want the result of split except that each string is contained in a list of length one.

Here's one approach.

orig = ['\nSK Telecom Co. Ltd. ADR\nSKM\n12/31/2021\n1.49\nN/A\nN/A']
result = [[s] for s in orig[0].split('\n')]

Or, if each entry of the list should be a list of characters,

orig = ['\nSK Telecom Co. Ltd. ADR\nSKM\n12/31/2021\n1.49\nN/A\nN/A']
result = [list(s) for s in orig[0].split('\n')]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Thomasi Muc
Solution 2 Ben Grossmann