I have a "samples" file containing numbers in two lines(separated by a single space):
12 24 36
45 56 67
I would like to add these two lines in python to two lists which list1 then becomes ['12','24','36'] and the list2 becomes ['45','56','67']. I have written code that extracts these in desired format. My problem here is I can not print these multiple lists with their index. For example how can I print only the first list ['12','24','36']. Below is the my code:
myfile=open('samples.txt','r')
for lines in myfile.readlines():
results=lines.split()
print(results)
Could you help me with this, please ?
with open('samples.txt','r') as myfile:
results = [line.split() for line in myfile]
print(results[0])
or if you just want the first line
with open('samples.txt','r') as myfile:
result = myfile.readline().split()
print(result)
to add them
print(sum(int(i) for i in result))