在格式化的文本文件中在python中创建对象

时间:2016-06-23 15:57:49

标签: python

我有一个格式如下的文本文件:

T    timestamp
U    username
W    text

有没有办法在T U和W属性的文本文件中创建对象?我只用Python编写脚本,没有任何面向对象,所以我有点迷失。

我需要解析这个巨大的文本文件,我正在逐行读取文件,寻找'W'属性,但是拥有整个对象可能更有用。

编辑:它看起来像这样

total number:18572084
T   2009-06-01 21:43:59
U   http://twitter.com/burtonator 
W   No Post Title

T   2009-06-01 21:47:23
U   http://twitter.com/burtonator
W   No Post Title

T   2009-06-02 01:15:44
U   http://twitter.com/burtonator
W   No Post Title

T   2009-06-02 05:17:52
U   http://twitter.com/burtonator
W   No Post Title

3 个答案:

答案 0 :(得分:0)

假设您的文档中的行是空格分隔的,您可以执行以下操作:

class YourObj(object):
    def __init__(self, t, u, w):
        self.t = t
        self.u = u
        self.w = w

all_objs = []

with open("somefile.txt") as f:
    lines = f.readlines()
    for i in range(len(lines)/3):
        all_objs.append(YourObj(t=lines[i], u=lines[i+1], w=lines[i+2])) 

all_objs  # all yours to work on now

答案 1 :(得分:0)

我喜欢卢克的答案,但在我看来,您需要更具体的格式来表达这种格式:

class YourObj(object):
    def __init__(self, dictionary): #init class with dictionary storing data
        self.T = dictionary['T']
        self.U = dictionary['U']
        self.W = dictionary['W']

all_objs = []
with open("somefile.txt") as f:
    lines = f.readlines()
    for i in range(0, len(lines), 3): #read lines in groups of three
        dic = {}
        for j in range(3):
            pieces = lines[i+j].split()
            dic[pieces[0]] = pieces[1] #save data to dictionary
        all_objs.append(YourObj(dic)) #make new object

答案 2 :(得分:0)

您只需要一种方法,但这里有两种变体和一个文件读取功能,它不能一次读取整个文件(最多可存储3-4行):

# **kwarg is a dictionary. It can hold any number of keyword arguments.
class obj1:
    def __init__(self, **kwarg):
        self.attributes = kwarg

# t,u,v as actual class attributes
class obj2:
    def __init__(self, t, u, w):
        self.t = t
        self.u = u
        self.w = w

objects1 = []
objects2 = []

with open("input_file", "r") as f:
    lines = []
    for line in f:
        line = line.strip()
        lines.append(line)
        if line.startswith("W"):
            objects1.append(obj1(t=lines[-3], u=lines[-2], w=lines[-1]))
            objects2.append(obj2(t=lines[-3], u=lines[-2], w=lines[-1]))
            lines = []


# same output but different ways of accessing the attributes

for o in objects1:
    print o.attributes["t"]
    print o.attributes["u"]
    print o.attributes["w"]

for o in objects2:
    print o.t
    print o.u
    print o.w

输入文件:

$ cat input_file 
total number:18572084
T   2009-06-01 21:43:59
U   http://twitter.com/burtonator 
W   No Post Title

T   2009-06-01 21:47:23
U   http://twitter.com/burtonator
W   No Post Title

T   2009-06-02 01:15:44
U   http://twitter.com/burtonator
W   No Post Title