-3

read the file and print list in ascending order of there names using python

my file content is like this

Summeet : 82736
David  : 55283
Vinay :22783
James :19923
Santosh :77283
Victor : 92384
Amit :66271
Harry : 38273
nbro
  • 15,395
  • 32
  • 113
  • 196
  • 1
    I am pretty sure this is homework. – nbro Jan 29 '16 at 13:52
  • Err... what you hope we do? – felipsmartins Jan 29 '16 at 13:52
  • Well, you could make a python dictionary. For each line you add the name as the key and the number as the value. You can the name and number by splitting each line by the character ":". When you are done reading the file you will then be able to sort the dictionary by key (the names). Then you can iterate over the dictionary and print each key and value. https://docs.python.org/2/tutorial/datastructures.html#dictionaries – haffla Jan 29 '16 at 13:55
  • 1
    Please show us the code you have already written and how it fails to do what you want. – Christian Geier Jan 29 '16 at 14:01
  • What is your specific issue? Your question is too broad. Do you know how to read text lines from a file in Python (`open()`, character encoding, `for line in file`)? Do you know how parse the input format (`.split(":")`)? Do you know how to sort text in Python? Do you need to take locale, Unicode properties into account? – jfs Jan 29 '16 at 14:45

1 Answers1

0

If this is not a homework and you need this in a linux system, don't use python but rather sort:

cat myfile.txt | sort

If you really need python, here are some tips:

  • create an array : arr = []
  • add each line to the array
  • use the sorted_arr = sorted(arr) method to sort the lines
  • print the sorted array
Derlin
  • 9,572
  • 2
  • 32
  • 53