【问题标题】:Print the results of a python file in bash on several lines在 bash 中多行打印 python 文件的结果
【发布时间】:2018-10-03 10:46:26
【问题描述】:

我有一个带有 for 循环的 python 文件。

说文件python.py:

for i in "Hello"
    print(i, '\n')

我在 .coffee 文件中将此代码作为 bash 命令运行

    command: "export LANG=en_CA.UTF-8; python3 folder/python.py"

    refreshFrequency: 1000*60

    style: """
      top: 245px
      right: 0px
      width: 240px
      height: 600px
      margin-left: -(@width / 2)
      overflow: hidden
      .content
        background: rgba(#fff, 0.5)
        color: #152033
        margin-left: 3px
        font-size: 18px
        font-family: Ubuntu
        text-align: left
      .title
        background: rgba(#fff, 0.5)
        color: #152033
        font-size: 40px
        font-family: Ubuntu
        text-align: center
      bg-blur = 10px
      .bg-slice
        position: absolute
        top: -(bg-blur)
        left: -(bg-blur)
        width: 100% + 2*bg-blur
        height: 100% + 2*bg-blur
        -webkit-filter: blur(bg-blur)
    """

    render: (output) -> """
      <canvas class='bg-slice'></canvas>
      <div class='title'>Inbox:</div>
      <div class='content'>#{output}</div>
    """

然后代码在一行上输出Hello。 如何让这段代码输出这样的东西?

H
e
l
l
o

我确实将'\n' 放在了 python 的打印命令中。

【问题讨论】:

    标签: python-3.x bash coffeescript


    【解决方案1】:

    你的python代码有问题,for循环后面没有冒号:。也不需要每行写“\n”,python会自动将每个字母移到新行:

    #! /usr/bin/python
    for i in "Hello":
        print(i)
    

    把它放在你的python脚本中,然后再次编译你的coffee脚本,它的输出为:

    H
    e
    l
    l
    o
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-04
      • 2016-03-08
      • 2022-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-03
      • 2017-03-10
      相关资源
      最近更新 更多