sorting - How do you sort by ascending order from a txt file in python? -
i new python , have simple question. have txt file road names , lengths. i.e. main street 56 miles. stumped on how call on file in python , sort road lengths in ascending order. help.
let's assume there "miles" after every number. (this untested code can edited think idea right).
edit: tested
import collections originaldict = {} newdict = collections.ordereddict() def isnum(string): try: if string ".": return true float(string) return true except exception: return false line in open(input_file, "r"): string = line[:line.find("miles") - 1] print string curnum = "" c in reversed(string): if not isnum(c): break curnum = c + curnum originaldict[float(curnum)] = [] originaldict[float(curnum)].append(line) num in sorted(originaldict.iterkeys()): newdict[num] = [] newdict[num].append(originaldict[num][0]) del originaldict[num][0] open(output_file, "a") o: value in newdict.values(): line in value: o.write(line + "\n")
Comments
Post a Comment