-5

我的问题是如何将对象填充到一些现有文件中。假设我的文件名是 "abc.txt" 。我的意思是 fname、lname、age、gender 都应该填充到文件中

#!usr/bin/python

class People:

    def __init__(self, fname=None, lname=None, age=None, gender=None):
        self.fname = fname
        self.lname = lname
        self.age = age
        self.gender = gender

    def display(self):
        print self.fname

    ppl1 = People('John','W','Cooper',23,'Male')
    ppl2 = People('Jack','A','Kalis',40,'Male')
    ppl1.display()
    ppl2.display()
4

5 回答 5

0

如果您只是希望将属性(逗号分隔)写入文件,您可以执行以下操作:

ppl = People('John', 'Cooper', 23, 'Male')

f = open('abc.txt', 'w')
ppl_attrs = [ppl.fname, ppl.lname, str(ppl.age), ppl.gender]
f.write(','.join(ppl_attrs))
f.close()
于 2013-10-17T10:24:46.050 回答
0

如此处python文档中所述,只需要写入文件的末尾(在检查它是否首先存在之后)。

于 2013-10-17T10:26:27.450 回答
0
class People:

    def __init__(self, fname=None, lname=None, age=None, gender=None):
        self.fname = fname
        self.lname = lname
        self.age = age
        self.gender = gender

def display(self):
    print self.fname

people = [People('John','W Cooper',23,'Male'),
           People('James','W Cooper',30,'Male'),
           People('Kate','W Cooper',20,'Female')]
f = open("abc.txt", "w")
for person in people:
    f.write( person.fname +","+ person.lname +","+ str(person.age) +","+ person.gender + '\n' )
    person.display()
f.close()
于 2013-10-17T10:26:28.877 回答
0
ppl = People('John W','Cooper',23,'Male')

with open("abc.txt", 'w') as f:
    f.write(",".join((ppl.fname, ppl.lname, str(ppl.age), ppl.gender)))
于 2013-10-17T10:30:31.703 回答
0

我只是想保存,文件没问题。但 db 是更好的选择。sqlite 是非常简单和好的数据库,它包含在 python 中。使用 sqlite 怎么样?

下面是示例代码。(对不起。快速而肮脏的代码)

#!usr/bin/python

import sqlite3

class People:

    def __init__(self, fname=None, lname=None, age=None, gender=None):
        self.fname = fname
        self.lname = lname
        self.age = age
        self.gender = gender
        self.save()

    def save(self):
        conn = sqlite3.connect('people.db')
        c = conn.cursor()
        c.execute("""create table if not exists
                     tbl_people(fname text, lname text,
                                age integer, gender text,
                                primary key (fname, lname) )""")
        c.execute("""insert or replace into tbl_people(fname, lname, age, gender)
                     values ('%s', '%s', %d, '%s')
                  """ % (self.fname, self.lname, self.age, self.gender) )
        conn.commit()
        conn.close()


    def display(self):
        print self.fname

def search(fname, lname):
    conn = sqlite3.connect('people.db')
    c = conn.cursor()
    c.execute("select * from tbl_people where fname='%s' and lname='%s'"%(fname,lname))
    ppl = c.fetchone()
    conn.close()
    if ppl is not None:
        return People(ppl[0], ppl[1], ppl[2], ppl[3])
    else:
        return None

ppl = People('John','Cooper',23,'Male')
ppl2 = People('Jane','Cooper',22,'Female')
ppl3 = search('John', 'Cooper')
ppl.display()
ppl3.display()
于 2013-10-17T10:42:59.320 回答