import re
file_contents = open("file.txt", "r").read() # read the contents
separator = re.compile(r'\s+', re.MULTILINE) # create a regex separator
numbers = []
for i in separator.split(f): # use the separator
try:
numbers.append(int(i)) # convert to integers and append
except ValueError: # if the item is not an integer, continue
pass
sorted_numbers = sorted(numbers)
现在可以将排序后的内容附加到另一个文件:
with open("toappend.txt", "a") as appendable:
appendable.write(" ".join(sorted_numbers)
你需要:
下面是一个代码示例,假设文件是空格分隔的,并且数字是整数:
现在可以将排序后的内容附加到另一个文件:
相关问题 更多 >
编程相关推荐