2016-07-06 61 views
2

編輯:此問題已得到解決。我想我會在這裏留給任何需要示例的未來人士。將對象列表添加到字典中

此代碼需要輸入文件,解析它,將文件傳遞給類,將對象存儲在字典中,然後以指定的格式寫出對象。有什麼問題嗎?我會很高興回答他們!

這裏是我的代碼:

#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 

%% ================= %% 
+0

嘗試在打印語句中刪除多餘的括號。將印刷品((Bambi.Species,Bambi.features,Bambi.diet,Bambi.threats))'印刷(Bambi.Species,Bambi.features,Bambi.diet,Bambi.reatreats)' –

+0

一點點!但是,如何將輸入文件作爲三個字符串輸出? – NewAtThis

回答

0
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)) 
0

可以通過添加行得到所有你的輸入文件中的行的:input_lines = inp.readlines()這將返回輸入文件中所有可以解析的行的列表。

+0

謝謝您的輸入!我已經使用這一行,現在已經訪問了輸入文件。現在我只需要弄清楚在文件上放置什麼樣的標準。我假設我將需要循環和if語句。 – NewAtThis

+0

對於如何解決這個問題你有什麼建議嗎? – NewAtThis