当我使用下面的代码时,我得到一个格式很差的输出。一个主要的输出问题是/n,/n不会出现在实际的文本文件中,但是从Python脚本中查看它,它都是“未格式化”的。在
代码:def start():
command = raw_input('''
1) Add
2) Look Up
3) See All
4) Delete Entry
''')
if command=="1":
add()
if command=="2":
look_up()
def add():
name = raw_input("What is your name?")
age = str(raw_input("How old are you?"))
salary = raw_input("Enter Salary:")
state = raw_input("State:")
fileObj = open("employees.txt","a")
fileObj.write("Name:"+name+"\n")
fileObj.write('--------------------------\n')
fileObj.write("Age:"+age+"\n")
fileObj.write("Salary:"+salary+"\n")
fileObj.write("State:"+state+"\n")
fileObj.write("--------------------------\n")
fileObj.write("\n\n")
fileObj.close()
print "The following text has been saved:"
print "Name:"+name
print "Age:"+age
print "Salary:"+salary
print "State:"+state
print "Note: This text was assigned to one line."
start()
def look_up():
fileObj = open("employees.txt")
line = fileObj.readlines()
print line
start()
start()
阅读和印刷的结果是:
['\n','姓名:诺亚\n','-----------------------------\n','年龄:16岁\n','工资:20000\n','状态:NC\n','-----------------------------\n','\n','\n','姓名:丹尼尔·雷尼\n','-----------------------------\n','年龄:18岁\n','工资:20万\n','状态:NC\n','-----------------------------\n','\n','\n','名称:fdadas\n','-----------------------------\n','年龄:343\n','工资:344433\n','状态:NC\n','-----------------------------\n','\n','\n']