获取格式错误的输出 (Python)
当我使用下面的代码时,我得到的输出格式非常糟糕。主要输出问题之一是 /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', 'Name:Noah\n', '------------------------ --\n', '年龄:16\n', '工资:20000\n', '州:NC\n', '-------------------- -------\n', '\n', '\n', '姓名:丹尼尔·雷尼\n', '-------------------- -------\n', '年龄:18\n', '工资:200000\n', '州:NC\n', '---------------- ------------\n', '\n', '\n', '名称:fdadas\n', '---------------- -----------\n', '年龄:343\n', '工资:344433\n', '州:NC\n', '------------ ----------------\n', '\n', '\n']
When I use the below code I get a very poorly formatted output. One of the main output problems are the /n. The /n don't show up in the real text file, but viewing it from the Python script it's all "unformatted".
The code:
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()
The outcome of reading and printing is:
['\n', 'Name:Noah\n', '--------------------------\n', 'Age:16\n', 'Salary:20000\n', 'State:NC\n', '--------------------------\n', '\n', '\n', 'Name:Daniel Rainey\n', '--------------------------\n', 'Age:18\n', 'Salary:200000\n', 'State:NC\n', '--------------------------\n', '\n', '\n', 'Name:fdadas\n', '--------------------------\n', 'Age:343\n', 'Salary:344433\n', 'State:NC\n', '--------------------------\n', '\n', '\n']
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在打印一个列表,这就是打印元素的原因。
尝试迭代它然后打印:
You are printing a
list
and that is why the elements get printed.Try iterating over it and then printing:
尝试使用
.read()
而不是.readlines()
:readlines()
将文件的行作为列表读取,read()
作为单个字符串。Try
.read()
instead of.readlines()
:readlines()
reads a the lines of the file as a list,read()
as a single string.