Reputation: 23
I have this fruit.txt which contains the following:
name, color
apple, red
banana, yellow
avocado, green
I managed to read each line of data using this code:
f =open('fruit.txt','r')
lines = f.readlines()
for oneline in lines:
oneline=oneline.strip()
oneline=oneline.split(',')
print(oneline)
It printed this result:
['name', ' color']
['apple', ' red']
['banana', ' yellow']
['avocado', ' green']
Since I need to assign the element from first list (name, color) as key for further task, how do I read the only the first list? Thank you.
Upvotes: 1
Views: 22