\ n不在输出中工作

时间:2018-03-09 13:48:12

标签: python newline spacing

您好我目前的输出为:

  

“根:$ 6 $ aYGtvxKp / bl6Fv2y $ sdZ3FbdJYQlP8VcfFZT.Y67We5EQmqcHW4I9Gl / 3pXp8v4.nu9qMIEkmOcdRuD0lBTvEtnMHosEo7OEaYgG4E0 :: 0:99999:7 ::: \ nbin::17110:0:99999:7 ::: \ ndaemon:< / em>的:17110:0:99999:7 ::: \ nadm::17110:0:99999:7 ::: \ NLP::17110:0:99999:7 ::: \的Nsync::17110:0:99999:7 ::: \ nshutdown::17110:0:99999:7 ::: \ nhalt::17110:0:99999:7 :: :\ nmail::17110:0:99999:7 ::: \ noperator::17110:0:99999:7个::: \ ngames::17110:0:99999: 7 ::: \ nftp::17110:0:99999:7 ::: \ nnobody::17110:0:99999:7 ::: \ nsystemd总线代理:!!:17572 :::::: \ nsystemd网络:!!:17572 :::::: \ ndbus:!!:17572 :::::: \ npolkitd:!!:17572 :::::: \ NTSS: !!:17572 :::::: \ nsshd:!!:17572 :::::: \ npostfix:!!:17572 :::::: \ nchrony:!!:17572 :::::: \时髦:$ 1 $ $ EgZiG263 4W / wMljYzhOqnupg9cJ7W /:17599:0:99999:7 ::: \ N'

从我的代码:

command = "cat /etc/shadow "
process = os.popen(command)
results = str(process.read())

我试图使它看起来像命令提示符中的那个,它是表格形式但由于某种原因,当我将它传递给python它确实做新的行函数“\ n”不起作用。我的代码出了什么问题?

2 个答案:

答案 0 :(得分:0)

您应该直接阅读该文件:

filename = '/etc/shadow'
with open(filename) as shadowfile:
    content = shadowfile.read()
    # or possibly lines = shadowfile.readlines()

您是否尝试打印输出,或者只是在解释器中看到结果变量的内容?在后一种情况下,换行符将显示为\ p,而print(results)将产生您期望的结果。

答案 1 :(得分:0)

l = 'root:$6$aYGtvxKp/bl6Fv2y$sdZ3FbdJYQlP8VcfFZT.Y67We5EQmqcHW4I9Gl/3pXp8v4.nu9qMIEkmOcdRuD0lBTvEtnMHosEo7OEaYgG4E0::0:99999:7:::\nbin::17110:0:99999:7:::\ndaemon::17110:0:99999:7:::\nadm::17110:0:99999:7:::\nlp::17110:0:99999:7:::\nsync::17110:0:99999:7:::\nshutdown::17110:0:99999:7:::\nhalt::17110:0:99999:7:::\nmail::17110:0:99999:7:::\noperator::17110:0:99999:7:::\ngames::17110:0:99999:7:::\nftp::17110:0:99999:7:::\nnobody::17110:0:99999:7:::\nsystemd-bus-proxy:!!:17572::::::\nsystemd-network:!!:17572::::::\ndbus:!!:17572::::::\npolkitd:!!:17572::::::\ntss:!!:17572::::::\nsshd:!!:17572::::::\npostfix:!!:17572::::::\nchrony:!!:17572::::::\funky:$1$EgZiG263$4W/wMljYzhOqnupg9cJ7W/:17599:0:99999:7:::\n'
for i in l.split('\n'):
 print(i)

输出:

root:$6$aYGtvxKp/bl6Fv2y$sdZ3FbdJYQlP8VcfFZT.Y67We5EQmqcHW4I9Gl/3pXp8v4.nu9qMIEkmOcdRuD0lBTvEtnMHosEo7OEaYgG4E0::0:99999:7:::
bin::17110:0:99999:7:::
daemon::17110:0:99999:7:::
adm::17110:0:99999:7:::
lp::17110:0:99999:7:::
sync::17110:0:99999:7:::
shutdown::17110:0:99999:7:::
halt::17110:0:99999:7:::
mail::17110:0:99999:7:::
operator::17110:0:99999:7:::
games::17110:0:99999:7:::
ftp::17110:0:99999:7:::
nobody::17110:0:99999:7:::
systemd-bus-proxy:!!:17572::::::
systemd-network:!!:17572::::::
dbus:!!:17572::::::
polkitd:!!:17572::::::
tss:!!:17572::::::
sshd:!!:17572::::::
postfix:!!:17572::::::
chrony:!!:17572::::::    unky:$1$EgZiG263$4W/wMljYzhOqnupg9cJ7W/:17599:0:99999:7:::