[Tutor] Tutor] string list in alphabetical!
bob gailer
bgailer at gmail.com
Mon Oct 21 20:10:32 CEST 2013
On 10/21/2013 12:16 PM, Siva Cn wrote:
> 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')
> ------------------------------------------------------------------------------------
I am surprised to see this program. It seems unnecessarily complex and
somewhat hard to read.
Especially for a rank beginner (the OP)
Also has an unnecessary statement (input_content = [])
IMHO it is more customary and a lot simpler to process the lines in a
file thusly:
for line in open(file1, 'r'):
process the line
--
Bob Gailer
919-636-4239
Chapel Hill NC
More information about the Tutor
mailing list