2017-03-22 18 views
-1

我有持有2动物信息 [('alyson', '5', 'dog'), ('merlin', '6', 'cat')] 我需要在列表重新排列动物品种,姓名,年龄的顺序,然后去除特殊字符,并将它们写回到列表一个.txt文件。 我试过.remove作为一个列表。我的下一个试验和错误是将列表转换为字符串,并尝试使用.replace方法。没有任何方法奏效。我可能错误地使用了它们。任何意见,我在哪里可以看看,非常感谢重排列表,并删除特殊字符

编辑:该程序是假设拉动动物信息的格式是:品种,名称,年龄。在我的一个功能中,我需要重新排列它并输出它的名字,年龄,品种。然后用户会提示动物是否被采纳或是否有新的动物进入,我的宠物列表会反映这一点。一旦用户完成,程序将把更新后的列表保存回原来的格式。我遇到了特殊字符的问题。我做pet_list [0],它会给我(“阿利森”的输出的打印测试,

#This function opens the file and arranges it into an output order 
#format of the file would be: dog, alyson, 5 
def get_pet(file): 
    f = open(file) 
    pet_info = f.readlines() 
    pet = [] 
    for line in pet_info: 
     info = line.split() 
     pet_type = info[0] 
     name = info[1] 
     age = info[2] 
     pet.append((name, age, pet_type)) 
    return pet 

#this is the function where i was planning on rearranging the list back to how it originally was and 
#rewrite them back onto a text file 
def do_save(pet_list, current_file, transfer, adopt_list, transfer_list): 
    shelter_list = open(current_file, 'w') 
    print(pet_list) 
    print(type(pet_list)) 
    for line in pet_list: 
     print(type(line)) 
     shelter_list.write(str(line)) 
    shelter_list.close() 
    adopt = open("adopted.txt", 'w') 
    adopt.write(str(adopt_list)) 
    adopt.close() 
    transfer_file = open(str(transfer), 'w') 
    transfer_file.write(str(transfer_list)) 

    transfer_file.close() 
+1

如果您希望人们提供帮助,您需要包括样本输入,期望的输出以及“什么都不工作”的描述。 –

+0

对不起,如果我没有更清楚。虐待编辑它以反映, –

+0

这是你想要的:'shelter_list.write(“”.join(line)+“\ n”)' – Barmar

回答

0

如果你只是想重新安排每个项目并写入文件。你可以字符串格式中的每个项目。pet_list要转换(品种,姓名,年龄)至(姓名,年龄,品种),你可以使用这样的事情:

pet_list = [('dog', 'alyson', '5'), ('cat','merlin','10')] 

for i in pet_list: 
    print("{1} {2} {0}".format(*i)) 

结果

alyson 5 dog 
merlin 10 cat 

在这里,我展示了如何打印,但而不是打印,你可以写入文件

+0

非常感谢你。 –

2

如果您有元组(或列表)的列表,您可以使用itemgetter通过多个键对其进行排序。与虚拟数据的一个例子:

import operator 
mylist=[ 
('an2d4', '1', 'kitty'), 
('an2d4', '2', 'kitty'), 
('an2d4', '3', 'kitty'), 
('an2d4', '1', 'puppy'), 
('an2d4', '2', 'puppy'), 
('an2d4', '3', 'puppy'), 
('[email protected]', '1', 'bear'), 
('[email protected]', '2', 'bear'), 
('[email protected]', '3', 'bear'), 
('[email protected]', '1', 'wombat'), 
('[email protected]', '2', 'wombat'), 
('[email protected]', '3', 'wombat') 
] 

mylist.sort(key=operator.itemgetter(2,0,1)) 

调用MYLIST则表明它已经被在第二指数(种)该项目第一排序,然后第0指数(名称),然后第一个指数(年龄)。

for i in mylist: 
    print i 

('[email protected]', '1', 'bear') 
('[email protected]', '2', 'bear') 
('[email protected]', '3', 'bear') 
('an2d4', '1', 'kitty') 
('an2d4', '2', 'kitty') 
('an2d4', '3', 'kitty') 
('an2d4', '1', 'puppy') 
('an2d4', '2', 'puppy') 
('an2d4', '3', 'puppy') 
('[email protected]', '1', 'wombat') 
('[email protected]', '2', 'wombat') 
('[email protected]', '3', 'wombat') 

然后,您可以编写一个函数来接受你的3项元组条目,并返回没有名称的特殊符号的输入,并通过你的名单列表进行迭代。

def fixName(entry): 
    name = entry[0] 
    fixedName = ''.join([x for x in name if x.isalpha()]) 
    return((fixedName, entry[1], entry[2])) 

mylist=[ fixName(x) for x in mylist] 

for i in mylist: 
    print i 

('bddy', '1', 'bear') 
('bddy', '2', 'bear') 
('bddy', '3', 'bear') 
('and', '1', 'kitty') 
('and', '2', 'kitty') 
('and', '3', 'kitty') 
('and', '1', 'puppy') 
('and', '2', 'puppy') 
('and', '3', 'puppy') 
('bddy', '1', 'wombat') 
('bddy', '2', 'wombat') 
('bddy', '3', 'wombat') 

然后迭代您的列表并写入文本文件,无论您想要的任何分隔符。

with open('out.txt', 'w') as outfile: 
    for entry in mylist: 
     outfile.write('\t'.join(entry)+'\n')