Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transpose a table of numbers in python

I need to write the data below in python,

student score
Bill         43
Suzy       54
Sally       41

but I have to flip it too look like

student Bill Suzy Sally
score    43   54    41

I have this written already,

ofile = open('grades','r')
Tfile = open('Tgrades','u')
date = []
while 1:
  a = ofile.readline()
  if a=='':
   break
  data.append(a.split())
like image 676
Alexander Michael Avatar asked Jan 13 '23 12:01

Alexander Michael


1 Answers

with open ('yourfile') as fin:
   lines = (line.split() for line in fin)
   for row in zip(*lines):
       print ' '.join(row)
like image 173
Jon Clements Avatar answered Jan 28 '23 01:01

Jon Clements