获取格式错误的输出 (Python)

发布于 2024-10-14 04:29:18 字数 1557 浏览 7 评论 0原文

当我使用下面的代码时,我得到的输出格式非常糟糕。主要输出问题之一是 /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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月染过的梦 2024-10-21 04:29:18
print line

您正在打印一个列表,这就是打印元素的原因。

尝试迭代它然后打印:

for ele in line:
    print ele
print line

You are printing a list and that is why the elements get printed.

Try iterating over it and then printing:

for ele in line:
    print ele
墨小沫ゞ 2024-10-21 04:29:18

尝试使用 .read() 而不是 .readlines()

def look_up():
    fileObj = open("employees.txt")
    contents = fileObj.read()
    print contents
    start()

readlines() 将文件的行作为列表读取,read() 作为单个字符串。

Try .read() instead of .readlines():

def look_up():
    fileObj = open("employees.txt")
    contents = fileObj.read()
    print contents
    start()

readlines() reads a the lines of the file as a list, read() as a single string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文