【问题标题】:Use awk in Spyder's IPython Console在 Spyder 的 IPython 控制台中使用 awk
【发布时间】:2018-11-01 08:43:31
【问题描述】:

我想从我的文件中删除多余的标题并使用以下内容:

awk '{ print $1}' test.txt > output.txt

然后我得到接下来的几行作为回应:

    File "<ipython-input-23-aa9db656210f>", line 1
    awk '{ print $1}' test.txt > output.txt
                    ^
SyntaxError: invalid syntax

不太确定我哪里出错了,我对 awk 命令有什么遗漏吗?我需要安装一些额外的软件包吗?我正在使用 Spyder IDE...

【问题讨论】:

  • Awk 是 bash 命令,而不是 python 命令。 bash 和 python 是不同的语言,所以在 python 中编码时不能使用 bash 命令。
  • @gbtimmon 不过,它不是 Bash 的一部分。它是一个单独的可执行文件,您可以从任何 shell 调用它。
  • 您可以在 IPython 中运行 bash 命令,方法是在它们前面加上 !。在这种情况下,这对应于!awk '{ print $1}' test.txt &gt; output.txt
  • 即使你不使用 IPython,也有办法在 Python 中访问你的外部 shell:stackoverflow.com/questions/89228/…
  • @BenjaminW。很好的澄清——我试图让我的回答保持在犯这个错误的人能理解的水平上,所以我故意把这个复杂性排除在外。

标签: python awk spyder


【解决方案1】:

来自Carlos Cordoba's comment

您可以在 IPython 中运行 bash 命令,方法是在它们前面加上 !。在这种情况下,这对应于!awk '{ print $1}' test.txt &gt; output.txt

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-09
    • 2018-01-05
    • 2016-04-15
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 2018-02-02
    • 1970-01-01
    相关资源
    最近更新 更多