beginner programmer
beginner programmer

Reputation: 23

python-access elements from the first list only

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

Answers (1)

user20340250
user20340250

Reputation:

Use

firstline = f.readline()

instead of f.readlines()

Upvotes: 2

Related Questions