0

I have a txt. file that I need to read and create a nested list. However some of details needs to be removed from the nested list. Please see the example below:

file1.txt

One Type

1

2

3

4

Friend

Sky

House

After Run the code I have to get:

(’type’, [1,2,3,4], \ [’Friend’, \ ’Sky’,\ ’House’])

  • What have you tried already? Could you please provide some more information to figure out what your problem is? – Carlos Horn Feb 22 '22 at 21:08
  • file1 = open("file1.txt","r") for line in file1: l.append(line.split()) print(l) – teuscast Feb 23 '22 at 15:33
  • Sorry, but this is not much helpful... So the question is, do you only need to read in one of those files? Then you can handcraft your solution. In case you know the pattern inside the file, e.g. after the word type, you expect numbers which go into a list, then words which go into a list..., then you could use regular expressions. So it is very important to be more specific on your input. – Carlos Horn Feb 23 '22 at 17:00
  • Hi, my the "file1.txt" that I have to read and run the code from these information has the details above in each row. However I need to create a nested loop to create this tuple with 1 string and two lists. – teuscast Feb 23 '22 at 17:39
  • (’type’, [1,2,3,4], \ [’Friend’, \ ’Sky’,\ ’House’]) = This is the result – teuscast Feb 23 '22 at 17:39

0 Answers0