标签:readlines ted time lists /usr 结果 ast 文件操作 操作
#把一个数字的list从小到大排序,然后写入文件,然后从文件中读取出来文件内容,然后反序,在追加到文件的下一行中 #!/usr/bin/env python # -*- coding:utf-8 -*- # @time: 2017/10/30 19:45 # Author: caicai # @File: sample01.py #import codecs import ast def listSort(a): b = sorted(a) return b def listReverse(file): a1 = file print(a1) for a2 in a1: a2 = list(ast.literal_eval(a2)) a2.reverse() return a2 def fileWrite(sort_list): f = open(‘2.txt‘,‘a‘) f.writelines(str(sort_list)) f.writelines(‘\n‘) f.close() return def fileRead(): fd = open(‘2.txt‘) a2 = fd.readlines() fd.close() return a2 if __name__ == ‘__main__‘: list1 = [2,32,43,453,54,6,576,5,7,6,8,78,7,89 ] list1_sort = listSort(list1) fileWrite(list1_sort) file_read = fileRead() file_read2 = listReverse(file_read) fileWrite(file_read2) print(fileRead())
输出结果: [‘[2, 5, 6, 6, 7, 7, 8, 32, 43, 54, 78, 89, 453, 576]\n‘, ‘[576, 453, 89, 78, 54, 43, 32, 8, 7, 7, 6, 6, 5, 2]\n‘]
标签:readlines ted time lists /usr 结果 ast 文件操作 操作
原文地址:http://www.cnblogs.com/wxp997/p/7758144.html