mwbr.net
当前位置:首页 >> Python对文本里面的内容排序 >>

Python对文本里面的内容排序

把文本的格式贴出来看看 假设格式是: 5KB 100KB 1MB 3MB 9MB 2MB 可用下面的程序(python3) units={"KB" : 1, "MB" : 1024}def calc(x): for unit, amount in units.items(): if x.find(unit)>0: number=int(x[:-2]) number*=amount return nu...

import os#your file pathfile_abs_path = 'newFile.txt'def get_info(oid, is_max): a, b = None, None with open(file_abs_path, 'rb') as f: for line in f.readlines(): # first line continue if line.strip()[0] == 'F': continue if int(...

感觉这就是一个标准的带分隔符的文件呀,可以试试把文件后缀名改为csv,用表格文件打开排序,搞完了在改回来。如果一定要写代码的话,那就将读取的每行数据,经过分割,删除换行符,转换格式的操作后,添加到一个新列表里,然后将新列表转换为pa...

path = 'E:\\PyWk\\nodepad_py\\' fp = open(path + 'm.txt') a = fp.readlines() a = [x.split('\t') for x in a] a = [[x[0],x[1].replace('\n','')] for x in a] print a fp.close() b = [[int(x[0]), int(x[1])] for x in a] b.sort() b = [...

文件这样子: 代码: fl = open('luciatest1.txt')l = []sortedl = []for lines in fl.readlines(): lines = lines.replace("\n", "") l.append(lines)print('\n', 'the original file is:', l)sortedl = sorted(l)print('\n', 'the sorted file...

list=['a','b','c','d','e','f','g']while list: print ' '.join(list[:4]) list = list[5:] # 输出 # a b c d# f g

a=open("cdays-4-test.txt","r") _a=open("cdays-4-result.txt","w") for b in a: if b.startswith("#"): continue c=b.split() if len(c)==0: continue else: _a.write(b) a.close() _a.close() 你程序中间为啥要运行一次result.sort()呢? 你...

主要使用到了csv包,来读取数据 排序什么的是相对容易的事 import csvwith open('xx.txt') as f: data = dict(csv.reader(f, delimiter=' ', skipinitialspace=True)) rst = sorted(data.items(), key=lambda x:x[1], reverse=True)with open('r...

def convert(filename): for ln in open(filename, 'r'): yield ' '.join(['abcd', ] + ln.strip().split('/'))import collectionscounter = collections.Counter(list(convert('file.txt')))for ln, times in counter.most_common(): print tim...

open打开,逐行加入到list里,用list自带的排序方法排序,然后按下标逐行写回到文件

网站首页 | 网站地图
All rights reserved Powered by www.mwbr.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com