Python按字段对文本文件排序

2024-04-26 01:44:45 发布

您现在位置:Python中文网/ 问答频道 /正文

按某个字段对文本文件中的数据进行排序时遇到问题。以后可能会有多个字段。txt是数千行代码。我是python新手,所以我的代码可能有点混乱。例如,这是我将从中读取的文本文件:

stuff
123 1200 id-aaaa stuart@test.com
322 1812 id-wwww machine-switch@test.com
839 1750 id-wwww gary2-da@test.com
500 0545 id-aaaa abc123@test.com
525 1322 id-bbbb zyx321@test.com

我的代码如下:

filelist = open("info.txt").readlines()
splitlist = list()

class data:
    def __init__(self, eventName, time, identity, domain):
        self.evenName = eventName
        self.time = time
        self.identity = identity
        self.domain = domain

for line in filelist:
    filelist = list.split(', ')
    splitlist.append(filelist)

for column in splitlist:
    if (len(column) > 1): #to skip the first line
        eventName = column[0].strip()
        time = column[1].strip()
        identity = column[2].strip()
        domain = column[3].strip()

我想按标识对.txt文件逐行排序,然后可能按时间排序。我在python教程中看到,这可以通过类来完成,所以我正试图走这条路。请告知。谢谢您!


Tags: 代码testselftxtcomidtime排序
3条回答
with open("info.txt") as inf:
    data = []
    for line in inf:
        line = line.split()
        if len(line)==4:
            data.append(line)

data.sort(key=lambda s:(s[2],s[1]))

如果你想变得更花哨一点

from collections import namedtuple
Input = namedtuple('Input', ('name', 'time', 'identity', 'domain'))

with open("info.txt") as inf:
    inf.next()  # skip header
    data = [Input(*(line.split()) for line in inf]

data.sort(key=lambda s:(s['identity'],s['time']))

如果你真的非常想使用一个类,请尝试:

import time

class Data(object):
    def __init__(self, event, time_, identity, domain):
        self.event = event
        self.time = time.strptime(time_, "%H%M")
        self.identity = identity
        self.domain = domain

with open("info.txt") as inf:
    data = []
    for line in inf:
        try:
            data.append(Data(*(line.split()))
        except TypeError:
            # wrong number of arguments (ie header or footer)
            pass

data.sort(key=lambda s:(s.identity,s.time))

按id然后按日期排序:

text = ["123 1200 id-aaaa stuart@test.com",
        "322 1812 id-wwww machine-switch@test.com",
        "839 1750 id-wwww gary2-da@test.com",
        "500 0545 id-aaaa abc123@test.com",
        "525 1322 id-bbbb zyx321@test.com"]
text = [i.split() for i in text]
text.sort(key=lambda line: (line[2],line[1]))
text = [' '.join(i) for i in text]
print text
#Output:
['500 0545 id-aaaa abc123@test.com', 
'123 1200 id-aaaa stuart@test.com', 
'525 1322 id-bbbb zyx321@test.com', 
'839 1750 id-wwww gary2-da@test.com', 
'322 1812 id-wwww machine-switch@test.com']

这是一个常见的错误,您所做的是在没有以正确语法读取文件的情况下打开它,下面是我的想法:

filelist = open("info.txt", "r")
print filelist
filelist.read() # reads the entire file
splitlist = list()

class data:
    def __init__(self, eventName, time, identity, domain):
        self.evenName = eventName
        self.time = time
        self.identity = identity
        self.domain = domain

for line in filelist:
    filelist = list.split(', ')
    splitlist.append(filelist)

for column in splitlist:
    if (len(column) > 1): #to skip the first line
        eventName = column[0].strip()
        time = column[1].strip()
        identity = column[2].strip()
        domain = column[3].strip()

希望能成功! 来源:http://docs.python.org/tutorial/inputoutput.html

相关问题 更多 >

    热门问题