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

2024-09-29 06:28:21 发布

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

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

T    timestamp
U    username
W    text

有没有办法在文本文件中创建tu和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

Tags: 对象notextcomhttp属性title格式
3条回答

您只需要一种方法,但这里有两种变体和单个文件读取函数,它不会一次读取整个文件(最多存储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

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

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

我喜欢卢克的回答,但在我看来,对于你的具体格式,你需要更像这样的东西:

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

相关问题 更多 >