2

テキスト ファイルのデータを特定のフィールドで並べ替えるのに問題があります。おそらく後で複数のフィールドによって。.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 ファイルを 1 行ずつ ID で並べ替え、次に時間で並べ替えたいと考えています。これは Python チュートリアルのクラスで実行できることがわかったので、その方法を試しています。お知らせ下さい。ありがとうございました!

4

4 に答える 4

8
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))
于 2012-06-10T22:49:38.557 に答える
0

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']
于 2012-06-10T22:55:36.027 に答える
0

次の Python コードは、必要な情報をまとめて並べ替えます。

rows = []
for line in open("info.txt"):
    line = line.split()
    if len(line) != 4:
        continue

    eventName, time, identity, domain = line

    # Add them in the order you want to sort by
    rows.append((identity, time, eventName, domain)) 

rows.sort()
于 2012-06-10T22:51:01.570 に答える
0

これはよくある間違いです。適切な構文で実際にファイルを読み取らずに開いた場合、次のようになります。

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

于 2012-06-10T22:52:26.447 に答える