I have a text file:
John|Hopkins|||31
Sage|Jen|42
And I want to read it into python and split by ‘|’
So I want something like:
[['John', 'Hopkins', '31'], ['Sage', 'Jen', '42']]
file = open('mytxt.txt', 'r')
file_2 = file.readlines()
lst=[]
for line in file_2:
line=line.strip('\n')
line=line.split('|')
lst.append(line)
print(lst)
I’m getting:
[['John', 'Hopkins', '', '', '31'], ['Sage', 'Gen', '42']]
As seen, there are ''
present in the first list due to consecutive ||
.
How do I modify the split statement to cater for single | and consecutive |||?