Python 实现文本文件多路归并排序

xiaoxiao2021-02-28  117

开发说明:前两天刚开始学习Python,但是厌烦了Hello World这类没用的东东,于是就自己边学边做,花了一天时间完成了这个稍微复杂的小应用;

麻雀虽小五脏俱全,作为初学者的例程还是很有指导意义的,真正做到从入门到精通,欢迎大家指正。

文本文件内容排序功能:

每行是一条记录,每行可以有多列,列间按预定义的分隔符分隔;

可以按单列或多列组合排序,每列的顺序可以设置为反序或者正序;

列的数据类型可以是字符串、整数、浮点数,比较排序时按指定的数据类型比较大小;

排序算法可以单线程执行(适用于小文件),也可以多线程执行(适用于大文件,分隔排序后再归并);

使用了如下技术要点:命令行参数面向对象字符串解析文件读取,写入多线程、线程池、队列、线程同步文件归并排序

导入的库:

import sysimport getopt

import threading

import time

import queue from functools 

import cmp_to_key from tempfile 

import TemporaryFile from datetime

import datetime

命令行说明:sort.py -i <input_filename> -o <output_filename> [-d <delimeter>] [-c <columns>] [-s <size>] [-t <threadCount>]

-i 输入源文件名

-o 输出目标文件名,如果未指定,则结果覆盖到源文件

-d 可选项,文件文本行的列分隔符,默认是空格

-c 可选项,相关排序列信息,包括列号(从1开始,按出现顺序优先级排序)、数据类型(i:整数,f:浮点数,默认:字符串)、是否反序(r),   默认按第一列字符串类型正序(升序)排序

-s 可选项,源文件分段最大行数,如果不指定则单线程执行,否则多线程执行排序

-t 可选项,线程数,指定-s参数时生效,默认值:2

示例:python sort.py -i d:/test.txt -o d:/test1.txt -c 4ir,3f,5 -s 10000 -t 3

关键的类:

class Column: # 列信息对象 # 列下标 index = 0 # 是否反序,默认正序 reverse = False # 列数据类型 String:0 Int:1 Float:2 # data_type = 0 def __init__(self, index, reverse, data_type): self.index = index self.reverse = reverse self.data_type = data_type @classmethod # 类方法 def parse(cls, s): reverse = False data_type = 0 min_index = len(s) i = s.find('r') if i > -1: reverse = True if i < min_index: min_index = i i = s.find('i') if i > -1: data_type = 1 if i < min_index: min_index = i i = s.find('f') if i > -1: data_type = 2 if i < min_index: min_index = i index = int(s[0:min_index]) - 1 return Column(index, reverse, data_type) class Line: # 行对象 # 行文本 # line = '' # 排序比较的列 # columns = [] def __init__(self, line, columns): self.line = line self.columns = columns @classmethod def parse(cls, line): all_columns = line.split(Config.delimeter) all_len = len(all_columns) columns = [] for column in Config.column_info_list: index = column.index if index < all_len: columns.append(all_columns[index]) return Line(line, columns)

排序比较函数的实现:

def cmp_line(l1, l2): # 比较函数 len1 = len(l1.columns) len2 = len(l2.columns) for i in range(len1): column_info = Config.column_info_list[i] if i >= len2: return -1 if column_info.reverse else 1 c1 = l1.columns[i] c2 = l2.columns[i] if column_info.data_type == 1: c1 = int(c1) c2 = int(c2) elif column_info.data_type == 2: c1 = float(c1) c2 = float(c2) if c1 > c2: return -1 if column_info.reverse else 1 elif c1 < c2: return 1 if column_info.reverse else -1 # len1 < len2 return 0 if len1 == len2 else 1 if Config.column_info_list[len1].reverse else -1

全部的代码下载链接

http://download.csdn.net/detail/u011606457/9834791

作品:http://bss.csdn.net/m/product/python_creative/opus

转载请注明原文地址: https://www.6miu.com/read-26080.html

最新回复(0)