I have a word list like this:
river
mississippi
water
spain
cairo
hellow
kind
words
sentences
They are separated by different number of '\n'
What I want to do is to put these words which separate by one '\n' in an inner list, and the words which separate by more than one (could be 2,3 or more) '\n' in different inner list like this:
[['river', 'mississippi', 'water', 'spain', 'cairo'], ['hellow','kind','words','sentences']]
I tried
infile=open(test_sets_file,'r')
readed=infile.readlines()
newlist=[]
new_nestedlist=[]
for i in range(len(readed)):
if readed[i]!='\n':
new_nestedlist.append(readed[i].strip('\n'))
else:
newlist.append(new_nestedlist)
new_nestedlist=[]
return newlist
it doesn't work My code cannot print anything when the input text is
river
mississippi
water
spain
cairo
I know it is because I initialized the list as an empty one when meet a '\n'
I also found another question Creating nested list from string data with two delimiters in Python about creating nested list by different separators, but it cannot solve my question