Is there a way to organize file content by list element in python? -


i working on project in python in regards file information on students. file orders students alphabetically last name in following format:

lastname,firstname,house,activity

the first direction change format to

firstname,lastname,house,activity

i have done this. next step organize them house that

amewolo, bob j.,e2,none anderson, billy d.,e1,basketball andrade, danny r.,e2,soccer banks-audu, rob a.,e2,football brads, kev j.,n1,band souza, ian l.,e1,eco club dimijian, annie a.,s2,speech , debate garcia, yellow,e1,none glasper, larry l.,n1,choir 

will output them organized house

amewolo, bob j.,e2,none andrade, danny r.,e2,soccer banks-audu, rob a.,e2,football anderson, billy d.,e1,basketball souza, ian l.,e1,eco club garcia, yellow,e1,none brads, kev j.,n1,band glasper, larry l.,n1,choir dimijian, annie a.,s2,speech , debate 

here code far

def main():     info = open('studentinfo.txt', 'r')     in info:         data =         data = data.rstrip('\n')         data = data.split(',')         print(format(data[1], '19s'),end='')         print(format(data[0], '19s'),end='')         print(format(data[2], '19s'),end='')         print(format(data[3], '19s'))  main() 

should use

data = data.sort(key = data[2]) 

or there way can sort particular list element

.sort() in-place, write:

data.sort(key=lambda item: item[2]) 

lambda item: item[2] shorthand for:

def get_sort_key(item):     return item[2] 

also, since file csv file, i'd use csv module.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -