1

I'm creating a program for a game that displays fixtures depending on the user input. Even though I have done this, when it displays the fixture it is just in one line. This causes it looks a bit messy and there's no way for users to know what each value means. What I want to do is display this in a table with the headings 'Fixture Number', 'Date Played', 'Player 1', 'Player Two', 'Was the fixture played?' and 'Winning Player'. A sample of the file is:

1,05/03/17,13:00,DarrenL,Philippa93,Y,DarrenL
2,06/03/17,13:00,TommyBoy,Paul4,Y,Paul4
3,07/03/17,13:00,Flip,Han68,Y,Han68

The code I have right now is:

fixFind = int(input("Please enter a fixture number: "))
if 189 >= fixFind >= 0:
    f = open("fixtures.txt", "r").readlines()
    lines = f[fixFind]
    print("""
    Fixture: """ + lines)
Chuck
  • 3,664
  • 7
  • 42
  • 76
Kyle341
  • 29
  • 1
  • 2
  • 4
  • Can you provide some sample data from your text file? – Chuck May 06 '17 at 14:03
  • You may want to refer to this answer: http://stackoverflow.com/a/15344226/6556102 – jedruniu May 06 '17 at 14:08
  • @Kyle341 Hey man, as you have not said that you can't import modules, I have given you an answer using the popular `pandas` module. It converts your file to a dataframe and then prints the output. You don't have to mess around with formatting. Let me know what you think :) – Chuck May 06 '17 at 14:35

2 Answers2

3

You can use tabs (the \t sequence) in your printed strings as a simplistic way of doing this. However, you will have to pay attention to column lengths and overrunning 80 characters on each line to keep your output lined up correctly.

fixFind = int(input("Please enter a fixture number: "))

print "Num\tDate Played\tTime\tP1\tP2\tPlayed?\tWinner"
if 189 >= fixFind >= 0:
  f = open("fixtures.txt", "r").readlines()
  lines = f[fixFind]
  for i in lines.split(","):
    print '%s\t' % i,

Output;

Num Date Played Time    P1      P2      Played? Winner
3   07/03/17    13:00   Flip    Han68   Y       Han68
Bert
  • 2,134
  • 19
  • 19
0

As OP has not specified imports are not posible, much easier to do this with pandas using read_csv

For a text file 'fixtures':

1,05/03/17,13:00,DarrenL,Philippa93,Y,DarrenL
2,06/03/17,13:00,TommyBoy,Paul4,Y,Paul4
3,07/03/17,13:00,Flip,Han68,Y,Han68

Specify columns:

columns = ['Fixture Number', 'Date Played', 'Time Played', 'Player 1', 'Player Two', 'Was the fixture played?', 'Winning Player']

Import pandas and read text file with no index, use columns as column names:

import pandas as pd
df = pd.read_csv("fixtures.txt", index_col=False, names=columns)

>>
   Fixture Number Date Played Time Played  Player 1  Player Two Was the fixture played? Winning Player
0               1    05/03/17       13:00   DarrenL  Philippa93                       Y        DarrenL
1               2    06/03/17       13:00  TommyBoy       Paul4                       Y          Paul4
2               3    07/03/17       13:00      Flip       Han68                       Y          Han68

User input for which column to keep, and print subset of dataframe:

fixture = int(input("Please enter a fixture number: "))

Return subset of dataframe for that that fixture number:

print df[df['Fixture Number'] == fixture]

>>
   Fixture Number Date Played Time Played Player 1  Player Two Was the fixture played? Winning Player
0               1    05/03/17       13:00  DarrenL  Philippa93                       Y        DarrenL

Documentation here: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html

Added benefit is you don't need the if statement.

Chuck
  • 3,664
  • 7
  • 42
  • 76