I having trouble reading a file. I have a small text file, like one underneath. My program is supposed to read the first string and store the rest of the integers and sort them in increasing order. I have to write my program in python, but I haven't able to figure out. How to separate my string and integers. Then in the end i have output the result with the name and sorted in integers.
radio
答案 0 :(得分:0)
您可以使用正则表达式和heapq
:
import re
import heapq
f = [i.strip('\n') for i in open('filename.txt')]
new_f = [re.findall("(?<=\d{1,}\.\s)[a-zA-Z0-9]+", i) for i in f]
def heapqueue_sort(numbers):
l = []
heapq.heapify(l)
for i in numbers:
heapq.heappush(l, i)
return [heapq.heappop(l) for i in range(len(l))]
final_data = [new_f[0]].extend(heapqueue_sort(map(int, new_f[1:]))]
print(final_data)