2016-01-16 152 views
0

我需要创建一个程序来保存人们的信息,例如他们的姓在一个文本文件中,具体取决于他们的姓氏的第一个字母,所以如果他们的姓以K开头,那么它将进入MyFile1如何在文件中写入新行?

我需要它像我做的那样循环,因为它是未知数量的人,但是我希望每个人在文本文件的不同行写入是有办法做到这一点。

底部的代码将每个单独的信息放到一个新行中,我不希望我希望每个不同的人都在一个新的行中。

MyFile1 = open("AL.txt", "wt") 
MyFile2 = open("MZ.txt", "wt") 
myListAL = ([]) 
myListMZ = ([]) 

while 1: 
    SurName = input("Enter your surname name.") 
    if SurName[0] in ("A","B","C","D","E","F","G","H","I","J","K","L"): 
     Title = input("Enter your title.") 
     myListAL.append(Title); 
     FirstName = input("Enter your first name.") 
     myListAL.append(FirstName); 
     myListAL.append(SurName); 
     Birthday = input("Enter birthdate in mm/dd/yyyy format:") 
     myListAL.append(Birthday); 
     Email = input("Enter your email.") 
     myListAL.append(Email); 
     PhoneNumber = input("Enter your phone number.") 
     myListAL.append(PhoneNumber); 
     for item in myListAL: 
      MyFile1.write(item+"\n") 

    elif SurName[0] in ("M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"): 
     Title = input("Enter your title.") 
     myListMZ.insert(Title); 
     FirstName = input("Enter your first name.") 
     myListMZ.append(FirstName); 
     myListMZ.append(SurName); 
     Birthday = input("Enter birthdate in mm/dd/yyyy format:") 
     myListMZ.append(Birthday); 
     Email = input("Enter your email.") 
     myListMZ.append(Email); 
     PhoneNumber = input("Enter your phone number.") 
     myListMZ.append(PhoneNumber); 
     line.write("\n") 
     for item in myListMZ: 
      MyFile2.write(line) 

    elif SurName == "1": 
     break 


MyFile1.close() 
MyFile2.close() 
+3

在您的问题描述中,您会说“我希望每个人都写在不同的行中”以及“我不希望我希望每个不同的人都在一个新的行中”。请澄清你的意思。 –

回答

2

您正在寻找join

当你有一个项目列表,你可以加入他们在一个单一的字符串。

l = ['a', 'b', 'c'] 
print(''.join(l)) 

产生

abc 

您不仅可以使用空字符串,但也是另一个字符串,将被用作分隔

l = ['a', 'b', 'c'] 
print(', '.join(l)) 

现在生产

a, b, c 

在你的例子(f或者例如第一write

MyFile1.write(','.join(MyListAL) + '\n') 

如果您正好有这不是一个字符串列表的东西:

MyFile1.write(','.join(str(x) for x in MyListAL) + '\n') 

(你也可以使用map,但发电机表达式就足够了)

编辑:添加map

MyFile1.write(','.join(map(str, MyListAL)) + '\n') 
+0

很好的答案。顺便说一句,gen expr不需要用括号括起来,就像任何其他表达式一样:'','.join(myListAL中用于x的str(x))' – Pynchia

+0

这是做我想做的事情谢谢但还有一个问题,它写在第一行的第一人的信息,然后写在第二行以及第二人的信息,如果你知道如何帮助我,请回复 – Thy

+0

对于第二个人,你必须申请'MyFile2.write(','。join(str(x)for MyListMZ)')。你甚至已经将一个'\ n'附加到列表中,所以它会被附加到字符串的末尾 – mementum

1

在你的情况下,我宁愿使用一个字典列表,其中一个人的所有信息都是一本字典。然后,您可以将其转换为JSON字符串,这是用于表示数据的标准格式。 (否则,你需要定义自己的格式,与项目之间的分隔符。)

因此,像这样:

import json # at the top of your script 

# I would create a function to get the information from a person: 
def get_person_input(): 
    person = {} 
    person["surname"] = input("Surname: ") 
    person["title"] = input("Title: ") 
    person["email"] = input("Email: ") 
    # TODO: do whatever you still want 

    return person 


# Later in the script when you want to write it to a file: 
new_line = json.dumps(person) 

myfile.write(new_line + "\n") 

解析JSON也很容易毕竟:

person = json.loads(current_line) # you can handle exception if you want to make sure, that it is a JSON format 

你可以在你的代码中使用它来决定在哪个数组中应该写这样的东西:

SurName = input("Enter your surname name.") 
if SurName[0] <= 'L': 
    ... 
else: 
    ... 

这将使您的脚本更清晰,更健壮。