Pexpect将“/r/n”插入发送行()

2024-06-07 07:45:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用pexpect在Ubuntu20.04的zsh终端上自动运行C程序。 正在讨论的程序是一个频谱转换器:http://www.np.ph.bham.ac.uk/research_resources/programs/spec_conv/spec_conv.c

我已经在我的路径中安装了它。我无法在终端中运行“spec_conv”,程序运行正常

当程序启动时,有一组初始选项(0-9)。我需要选择5个。第二个选项是单击“Y”。然后程序要求输入文件名。我有一个名为“file_list”的文件,我在终端中键入该文件,频谱按预期处理

我正试图用python实现这一点的自动化。到目前为止,我的代码是:

import pexpect
child = pexpect.spawn('spec_conv')
child.sendline('5')
child.sendline('y')
child.sendline('file_list')
print(child.read())

代码似乎无法读取文件。print(child.read())的输出是:

b'\r\n \t \t    *****Welcome to SPEC_CONV*****\r\n\tThis program converts spectra between RadWare, Ascii,\r\n\tXtrack (GASPWARE) and Ortec (binary Chn & ASCII Spe) formats,\r\n\tincluding multiple-spectra (<999) Xtrack files, e.g. from AGATA.\r\n\tand can gainmatch spectra.\r\n\t(Ascii means (y) or (x y) data starting from channel zero)\r\n\tComment lines starting with # are ignored at the front of\r\n\tascii spectra. The 1 or 2 col. format is auto-detected.\r\n\r\n 1) to convert RadWare (.spe) ==> Ascii (.txt)\r\n 2) to convert Ascii (.txt) ==> RadWare (.spe)\r\n 3) to convert Ascii (.txt) ==> Xtrack (.spec)\r\n 4) to convert Maestro_Chn (.Chn) ==> Ascii (.txt)\r\n 5) to convert Maestro_Chn (.Chn) ==> RadWare (.spe)\r\n 6) to convert Xtrack (.spec) ==> Ascii (.txt)\r\n 7) to convert Xtrack (.spec) ==> RadWare (.spe)\r\n 8) to convert GENIE (.IEC) ==> RadWare (.spe)\r\n 9) to convert Maestro_Spe (.Spe) ==> RadWare (.spe)\r\n a) to convert Maestro_Spe (.Spe) ==> Ascii (.txt)\r\n g) to gainmatch a RadWare spectrum\r\n 0) Quit\r\n5^J\r\nRead spectrum names from list file (y/n) \r\ny^J\r\nType filename containing list of spectrum file names:\r\nCannot open file:  \r\nfile_list\r\n'

正如您在本摘录的最后所看到的,它将文件名读取为“\r\n文件列表\r\n”,因此无法找到该文件。我曾尝试过其他类似问题中提出的两种解决方案,但均未奏效:

https://github.com/pexpect/pexpect/issues/238Preventing linewrap when using pexpect / bash

正在添加setwinsize:

import pexpect
child = pexpect.spawn('spec_conv')
child.setwinsize(1000,1000)
child.sendline('5')
child.sendline('y')
child.sendline('file_list')
print(child.read())

输出是相同的

我还尝试通过添加建议的“--noediting”来更改.spawn()输入:

import pexpect
child = pexpect.spawn('spec_conv --noediting')
child.setwinsize(1000,1000)
child.sendline('5')
child.sendline('y')
child.sendline('file_list')
print(child.read())

这给出了一个先前失败的输出,我不完全理解其原因:

b'\r\n \t \t     *****Welcome to SPEC_CONV*****\r\n\tThis program converts spectra between RadWare, Ascii,\r\n\tXtrack (GASPWARE) and Ortec (binary Chn & ASCII Spe) formats,\r\n\tincluding multiple-spectra (<999) Xtrack files, e.g. from AGATA.\r\n\tand can gainmatch spectra.\r\n\t(Ascii means (y) or (x y) data starting from channel zero)\r\n\tComment lines starting with # are ignored at the front of\r\n\tascii spectra. The 1 or 2 col. format is auto-detected.\r\n\r\n\r\nUnrecognised arguments...usage: spec_conv\r\n or: spec_conv SpectrumFileName\r\n ***File --noediting does not exist\r\n5\r\ny\r\nfile_list\r\n'

Tags: tochildconvertasciilistfilepexpectradware
1条回答
网友
1楼 · 发布于 2024-06-07 07:45:52

如果您要手动运行生成的程序,您应该能够看到,当您回答y/n问题时,您只需要键入y即可立即获得答案,而无需回车

因此,您需要发送单个字符,而不是使用sendline()向发送的字符串添加换行符。替换

child.sendline('y')

child.send('y')

相关问题 更多 >

    热门问题