No, this wont work. You shouldnt do a = a.append(b)
, just a.append(b)
. This works:
# file '1.py'
import sys
def main():
tuples_list = []
for item in sys.stdin:
item = tuple(item)
tuples_list.append(item)
print(tuples_list)
main()
#file 'inp.txt'
1 2 3 4
1 2 4 3
3 3 3 3
$ python 1.py <inp
[('1', ' ', '2', ' ', '3', ' ', '4', ' ', '\n'), ('1', ' ', '2', ' ', '4', ' ', '3', '\n'), ('3', ' ', '3', ' ', '3', ' ', '3', '\n')]
But, in my opinion, using self-written procedures for file input is a bad idea. As you can see, this code reads characters, not numbers, so you need to fix it manualy, which leads to more code, with bugs, of course, debugging, waste of time, etc. You may want to do instead something like this:
# file inp.txt
1,2
3,4
5,6
import numpy as np
data = np.genfromtxt("inp.txt", delimiter=",")
print(data)
This will give you:
array([[1., 2.],
[3., 4.],
[5., 6.]])
This code is way faster than anything you will write(no, see comments below), because it uses numpy functions written in C/Fortran, way simpler to read, it handles any number of digits in line, and need no debugging. Using libraries are often better solution then writing code by yourself.