[Tutor] Tutor] string list in alphabetical!

Siva Cn cnsiva.in at gmail.com
Mon Oct 21 18:16:37 CEST 2013


Hi Sammy,

Try this this may help you !

----------------------------------------------------------------------------------
def sort_file1_to_file2(file1, file2):
    """."""
    input_content = []
    with open(file1, 'r') as fp:
        input_content = fp.read()

    input_content = input_content.splitlines()

    _dict = {ele[0].lower(): ele for ele in input_content}

    out_content = "\n".join([_dict[chr(idx)]
                             for idx in range(97, 123)
                             if chr(idx) in _dict])

    with open(file2, 'w') as fp:
        fp.write(out_content)

sort_file1_to_file2('file1.txt', 'file2.txt')
------------------------------------------------------------------------------------

*-- Regards --*
*
*
*   Siva Cn*
*Python Developer*
*
*
*http://www.cnsiva.com*
---------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20131021/3939ae35/attachment-0001.html>


More information about the Tutor mailing list