For example I have a txt file:
3 2 7 4
1 8 9 3
6 5 4 1
1 0 8 7
On every line there are 4 numbers and there are 4 lines. At end of lines there's \n (except the last one). The code I have is:
f = input("Insert file name: ")
file = open(f, encoding="UTF-8")
What I want is the text file to become [[3,2,7,4],[1,8,9,3],[6,5,4,1],[1,0,8,7]].
I have tried everything, I know the answer is probably really simple, but I just really give up after an hour of attempts. Tried read(), readlines(), split(), splitlines(), strip() and whatever else I could find on the internet. So many can't even make a difference between them...
withstatement for opening the files. It will close the file object at the end of the block automatically. Beside, you should use consider usingcsvmodule which is more suitable for this case. Read your file withcsvand it will gives you an iterable contain all lines splitted. Thenlist(reader_object)will give you the desire result.