【问题标题】:Python. sed -e expression #1: char 11: unterminated 's' commandPython。 sed -e 表达式 #1:char 11:未终止的“s”命令
【发布时间】:2017-07-10 15:15:30
【问题描述】:

我在 python 中执行 shell 命令时遇到问题。这是导致错误的代码的某些部分:

p = subprocess.Popen(["cat input.txt |apertium -d. kaz-morph|\
    sed -e 's/\$\W*\^/$\n^/g'| cut -f2 -d'/'|cut -f1 -d '<'|\
    awk '{print tolower($0)}'|sort -u>output.txt"], shell=True, stdout=f1)

仍然收到错误:未终止的“s”命令。

希望你能帮助我,因为我解决了 10 天 :(

附言对不起我的英语

【问题讨论】:

  • 另外,用shell=True,只要一个字符串就可以了,不需要使用字符串列表
  • 将该脚本放入单独的文件中可能会更干净,例如~/morph-and-sort.sh 并执行 chmod +x ~/morph-and-sort.sh 并将 python 更改为 Popen(["~/morph-and-sort.sh"], shell=True,stdout=f1)

标签: python shell sed


【解决方案1】:

'\n' 必须是 '\\n',否则它会被解释为换行符,从而导致未终止的字符串 "cat input.txt |apertium -d. kaz-morph|sed -e 's/\$\W*\^/$"

或者,将字符串标记为原始字符串:r"cat input.txt |apertium ...."

【讨论】:

    猜你喜欢
    • 2020-11-25
    • 1970-01-01
    • 1970-01-01
    • 2021-06-26
    • 2019-03-17
    • 1970-01-01
    • 2021-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多