Mert
Mert

Reputation: 45

How do I print certain parts of a list?

Here is my code:

def option_A():
 print("Pick a Fixture!")
 fixture_choice = int(input("Enter: "))

 file = open("firesideFixtures.txt", "r")
 fixture_number = file.readlines(fixture_choice)
 fixture = [linecache.getline("firesideFixtures.txt", fixture_choice)] 
 print(fixture)
 file.close()

The first line from the file I am using is:

1,02/09/15,18:00,RNGesus,Ingsoc,Y,Ingsoc

The expected result is:

1, 02/09/15, RNGesus, Ingsoc, Y, Ingsoc

The result I get:

['1,02/09/15,18:00,RNGesus,Ingsoc,Y,Ingsoc\n']

How can I do this?

Upvotes: 1

Views: 66

Answers (1)

Mike Müller
Mike Müller

Reputation: 85442

Print the only element of your list by indexing into it:

print(fixture[0])

Output:

1,02/09/15,18:00,RNGesus,Ingsoc,Y,Ingsoc

Or, even better don't create a list in the fist place (note the missing []):

fixture = linecache.getline("firesideFixtures.txt", fixture_choice) 

How can I remove the "18:00" part from the output because all I need is "1,02/09/15, RNGesus, Ingsoc, Y, Ingsoc" (from comment)

Now, remove the time:

fixture = linecache.getline("firesideFixtures.txt", fixture_choice) 
parts = fixture.split(',')
res = ','.join(parts[:2] + parts[3:])
print(res)
print(fixture)

Output:

1,02/09/15,RNGesus,Ingsoc,Y,Ingsoc

Upvotes: 1

Related Questions