我制作了一个程序,可以在文本文件中输出用户名和ID(这个文件叫做task_3.1)。
membership_data = open("C:\\Users\\user\Desktop\Pre-release\membership_data.txt", "w")
def ValidateMemberID(MemberID):
if len(MemberID) !=6:
return("Wrong Format")
elif MemberID[:1] == (MemberID[:1]).lower():
return("Wrong Format")
elif MemberID[1:3] == (MemberID[1:3]).upper():
return("Wrong Format")
elif (MemberID[3:]).isdigit() == False:
return("Wrong Format")
else:
return("Correct Format")
def inputdata():
Name = input("Please input member name")
MemberID = input("Please input member ID")
ValidateMemberID(MemberID)
if ValidateMemberID(MemberID) == "Correct Format":
NameID = [Name," ", MemberID, "\n"]
else:
print ("Invalid MemberID")
membership_data.writelines(NameID)
for x in range(2):
inputdata()
membership_data.close()
它工作正常,这是结果文本:
Ammar Amm123
Ammar Amm123
Ammar Amm123
Ammar Amm123
Ammar Amm123
我的问题是我需要将这个文本格式化为两列,每列都有一个标题,我不知道如何。我试着复制我朋友的代码并修改它以适应我的但它不会工作。
我最终得到了这个但是使用导入功能我得到一个语法错误而没有导入我没有得到我想要的东西。
from task_3.1 import NameID
Name = []
MemberID = []
membership_data = open("C:\\Users\\user\Desktop\Pre-release\membership_data.txt", "a")
for line in (NameID):
NameID = line.split()
Name.append(NameID[0])
MemberID.append(NameID[1])
membership_data.writelines("Name Member ID")
for i in range(len(Name)):
print ("%-*s %s" % (20, Name[i], MemberID[i]))
membership_data.writelines(Name)
membership_data.close()