Вот примерная функция, которая делает это с комментариями о том, что делается в строке:
# open the file with a with statement so that
# it automatically closes via its context manager
# when we exit the context
with open('file.txt', 'r') as f:
# We will enumerate the file handler
# So that we can easily tell which number
# is odd or which is even.
# Note that enumerate starts at 0, not 1
for num, line in enumerate(f):
# Make sure no spaces, line breaks, etc. at the start/end of each line.
# The list separator is a space (default) so `line.split()`
# will accomplish this.
line = line.strip()
line_as_list = line.split()
# For the second, forth, etc. lines we want to grab the list
if num % 2 == 1:
print ('EVEN -- %s' % line_as_list)
# On the other lines, if it has three items separated by a space
# we will grab those three items (again using `line.split()` and
# assign a, b, and c as the (arbitrary) variable names to those
elif len(line_as_list) == 3:
a,b,c = line_as_list
print ('ODD -- a=%s, b=%s, c=%s' %(a,b,c))
# Depending on what you want to do if there are not three variables
# you would handle them in this section here.
else:
print ('ODD -- BUT NOT THREE VARS')
# ODD -- a=1, b=2, c=10
# EVEN -- ['10', '9']
# ODD -- a=1, b=3, c=10
# EVEN -- ['2', '9', '5']
Вышеприведенное, вероятно, может быть сжато в пару строк кода, но оно было подробно написано выше.