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!
CodePudding user response:
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'))
CodePudding user response:
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')]