I’m having trouble splitting text in a data file such that suppose the the data file consisted of:
Row 1
apple
bob
cat
dog
ear
fun
Row 2
glow
horse
idea
joke
kick
lemon
Row 3
money
new
odd
park
queen
run
I want to split it so that it becomes a nested list like the following:
[[apple, bob], [cat, dog], [ear, fun]],
[[glow, horse], [idea, joke], [kick, lemon]],
[[money, new], [odd, park], [queen, run]]
This is my work so far:
def text_file(data_file):
nested_list = []
main_list = []
my_list = ''
for index in data_file:
index = index.strip()
if (index in my_list):
main_list.append(nested_list)
nested_list = []
else:
nested_list.append(index)
if (nested_list):
main_list.append(nested_list)
return (main_list)
but this returns:
text_file(open("data_file.txt", "r"))
[['Row 1', 'apple', 'bob', 'cat', 'dog', 'ear', 'fun'],
['Row 2', 'glow', 'horse', 'idea', 'joke', 'kick', 'lemon'],
['Row 3', 'money', 'new', 'odd', 'park', 'queen', 'run']]
Without importing anything, how can I achieve this? If possible what can I add into my code?
What you need to do is split the file by
\n\n(two newlines) which will give you the groups, then split the result of that by line, then usezipto step over the file appropriately to build your required lists, an eg: