python subprocess标准输入在第一个空格处被截断。

huangapple go评论111阅读模式
英文:

python subprocess stdin truncated at first space

问题

我对pOpen.communicate()有一个问题,就是我发送给子进程的字符串在第一个空格字符处截断(请记住,我使用utf-8编码为字节)。

输入文件text.txt

  1. this is an input string

我的代码:

  1. from subprocess import Popen, PIPE
  2. # 打开文件并将其编码为utf-8字节
  3. fp = open('test.txt')
  4. inputs = fp.read()
  5. inputBytes = bytes(inputs, 'utf-8')
  6. # 打开子程序并将字节发送到STDIN
  7. p = Popen(['./program'], stdout=PIPE, stdin=PIPE, stderr=PIPE)
  8. stdout_data = p.communicate(input=inputBytes)
  9. # 打印子进程的STDOUT,将打印给定的内容
  10. out = str(stdout_data).split('\n')
  11. for frame in out:
  12. print(frame)

我的预期输出:

  1. (b'this is an input string', b'')

我得到的输出:

  1. (b'this', b'')

我是否使用了错误的编码格式?子程序是一个使用scanf监听pOpen.communicate()数据的Go应用程序。

  1. package main
  2. import "fmt"
  3. func main() {
  4. var inputs string
  5. fmt.Print("")
  6. fmt.Scanf("%s", &inputs)
  7. fmt.Println(inputs)
  8. }
英文:

I have an issue with pOpen.communicate() where the string I send to the subprocess truncates at the first occurrence of a space character I am sending to STDIN (keep in mind I am encoding to bytes using encoding utf-8)

Input file text.txt

  1. this is an input string

My code

  1. from subprocess import Popen, PIPE
  2. # open file and encode to utf-8 bytes
  3. fp = open('test.txt')
  4. inputs = fp.read()
  5. inputBytes = bytes(inputs, 'utf-8')
  6. # open subprogram and send the bytes to STDIN
  7. p = Popen(['./program'], stdout=PIPE, stdin=PIPE, stderr=PIPE)
  8. stdout_data = p.communicate(input=inputBytes)
  9. # print STDOUT of subprocess, will print what was given
  10. out = str(stdout_data).split('\\n')
  11. for frame in out:
  12. print(frame)

my expected output:

  1. (b'this is an input string', b'')

what I got:

  1. (b'this', b'')

am I using the wrong encoding format? The subprogram is a go application that uses scanf to listen to pOpen.communicate() data

  1. package main
  2. import "fmt"
  3. func main() {
  4. var inputs string
  5. fmt.Print("")
  6. fmt.Scanf("%s", &inputs)
  7. fmt.Println(inputs)
  8. }

答案1

得分: 1

Go的fmt.Scanf基于C的scanf%s会跳过前导空格,然后读取下一个单词直到第一个空格。这可能不是你想要的。

根据文档,更加精确的解释是:

由动词处理的输入隐式以空格分隔:除了%c之外的每个动词的实现都会从剩余输入中丢弃前导空格,而%s动词(以及读入字符串的%v)会在第一个空格或换行符处停止消耗输入。(https://pkg.go.dev/fmt)

英文:

Go's fmt.Scanf is based on C's scanf; %s skips leading spaces and then reads the next word up to the first space. That's probably not what you wanted.

From the docs, a bit more precise:

> Input processed by verbs is implicitly space-delimited: the implementation of every verb except %c starts by discarding leading spaces from the remaining input, and the %s verb (and %v reading into a string) stops consuming input at the first space or newline character. (https://pkg.go.dev/fmt)

huangapple
  • 本文由 发表于 2022年12月9日 09:57:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/74738188.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定