Python 2.7 将对象列表添加到词典

Python 2.7 将对象列表添加到词典,python-2.7,object,dictionary,key,Python 2.7,Object,Dictionary,Key,编辑:此问题已解决。我想我会把这个留给未来需要榜样的人 这段代码接收输入文件,对其进行解析,将文件传递给类,将对象存储在字典中,然后以指定格式写出对象。有什么问题吗?我很乐意回答他们 这是我的密码: #C:\Python27\python.exe Animals(1).py inp = open("C:\Users\curnutte\Desktop\Assignment\Python Scripts\Python example\Classes\ClassTutorialInput.txt",

编辑:此问题已解决。我想我会把这个留给未来需要榜样的人

这段代码接收输入文件,对其进行解析,将文件传递给类,将对象存储在字典中,然后以指定格式写出对象。有什么问题吗?我很乐意回答他们

这是我的密码:

#C:\Python27\python.exe Animals(1).py

inp = open("C:\Users\curnutte\Desktop\Assignment\Python Scripts\Python example\Classes\ClassTutorialInput.txt", "r")
outp = open("C:\Users\curnutte\Desktop\Assignment\Python Scripts\Python example\Classes\ClassTutorialOutput.txt", "w")

aniList = []

for line in inp.readlines():
    if line == '\n':
        line = "nothing"
    line = line.strip()                                                        #removes '\n'
    if line != '%% ================= %%':                                       #grabs all pertinent information
        aniList.append(line)                                                    #condenses lines to one line
print(aniList)

class Animals():                                                                 #creates the Animals class
    def __init__(self, species, features, diet, drinks, threats):
        self.species = species
        self.features = features
        self.diet = diet
        self.drinks = drinks
        self.threats = threats

my_animals=[]

counter = 0
for item in aniList:                                                            #iterates through the input file
    counter += 1
    if counter == 1:
        species = item
        continue
    if counter == 2:
        features = item
        continue
    if counter == 3:
        diet = item
        continue
    if counter == 4:
        drinks = item
        continue
    if counter == 5:
        threats = item
        my_animals.append(Animals(species, features, diet, drinks, threats))
        counter = 0
        continue

animal_dict = {}

for index in my_animals:
    animal_dict[index.species] = (index)
    print (animal_dict)
    oldLook = index.features.split(",")
    newLook = index.features
    if len(oldLook) == 2:
        newLook = oldLook[0].strip() + ' and ' + oldLook[1].strip()
    if len(oldLook) > 2:
        newLook = oldLook[:-1]
        newLook = ",".join(newLook)
        newLook = newLook + ' and ' + oldLook[-1].strip()
    index.features = newLook

    oldDiet = index.diet.split(",")
    newDiet = index.diet
    if len(oldDiet) == 2:
        newDiet = oldDiet[0].strip() + ' and ' + oldDiet[1].strip()
    if len(oldDiet) > 2:
        newDiet = oldDiet[:-1]
        newDiet = ",".join(newDiet)
        newDiet = newDiet + ' and ' + oldDiet[-1].strip()
    index.diet = newDiet

    oldPred = index.threats.split(",")
    newPred = index.threats
    if len(oldPred) == 2:
       newPred = oldPred[0].strip() + ' and ' + oldPred[1].strip()
    if len(oldPred) > 2:
       newPred = oldPred[:-1]
       newPred = ",".join(newPred)
       newPred = newPred + ' and ' + oldPred[-1].strip()
    index.threats = newPred
    outp.write("A %s has %s, eats %s, drinks %s, and is eaten by %s.\n" % (index.species, index.features, index.diet, index.drinks, index.threats))

inp.close()
outp.close()
下面是“ClassTutorialInput.txt”:

deer
fur, antlers, a peaceful disposition
grass, leaves, roots
water
wolves, bear, mountain lions
%% ================= %%
fish
scales, a clueless lifestyle
bugs
water
sharks, humans
%% ================= %%
lion
fur, manes, a blood-thirst
antelope, humans
water

%% ================= %%
human
hair, skin
vegetables, meat
water

%% ================= %%

您可以通过添加以下行来获取输入文件中的所有行:
input\u lines=inp.readlines()
,该行将返回输入文件中所有行的列表,然后您可以解析这些行

尝试删除打印语句中的额外括号。将
打印((Bambi.species,Bambi.features,Bambi.diet,Bambi.threats))
更改为
打印(Bambi.species,Bambi.features,Bambi.diet,Bambi.threats)
这样做会让事情变得干净一点!但是,我怎样才能把输入文件写成三个字符串呢?谢谢你的输入!我已经使用了这一行,现在已经访问了输入文件。现在我只需要弄清楚在文件中放置什么样的标准。我假设我需要循环和if语句。您对如何实现这一点有什么建议吗?
for index in my_animals:
oldDiet = index.diet.split(",")
newDiet = index.diet
if len(oldDiet) == 2:
    newDiet = oldDiet[0].strip() + ' and ' + oldDiet[1].strip()
if len(oldDiet) > 2:
    newDiet = oldDiet[:-1]
    newDiet = ",".join(newDiet)
    newDiet = newDiet + ' and ' + oldDiet[-1].strip()
index.diet = newDiet

oldPred = index.threats.split(",")
newPred = index.threats
if len(oldPred) == 2:
    newPred = oldPred[0].strip() + ' and ' + oldPred[1].strip()
if len(oldPred) > 2:
    newPred = oldPred[:1]
    newPred = ",".join(newPred)
    newPred = newPred + ' and ' + oldPred[-1].strip()
index.threats = newPred
outp.write("A %s has %s, eats %s, and is eaten by %s.\n" % (index.species, index.features, index.diet, index.threats))