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...

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 = [...

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...

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

文件这样子: 代码: 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...

这个代码很简单 #!/usr/bin/env python3data = dict()# process a.txtfor line in open("a.txt", "r"): k, s, v = line.partition("_") if s != "_": continue data[k.strip()] = v.strip()# generate c.txtfd = open("c.txt", "w")for line in ...

import string import math file=open('top250.txt','r') file.readline() file.readline() order=range(250) for i in order: s=file.readline() k=s.split() order[i]=k year=int(raw_input('please input a year')) total=0 for m in order: ...

def readFile():outputData = []f = open(r"you file path")for i in f.readlines():outputData.append(i.strip('\n').split(' ')[::-1])f.close()return outputDatadef writeFile():allData = readFile()dataToStr = '\n'.join(['\n'.join([i f...

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

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