使用 python 列表作为使用 stdin 作为输入的 linux 命令的输入
我正在使用 python 脚本将数据加载到数据库批量加载器。
加载器的输入是 stdin。我无法获得正确的语法来调用基于 unix 的批量加载器并传递要加载的 python 列表的内容。
我一直在阅读有关 Popen 和 PIPE 的内容,但它们的表现并不符合我的预期。
python 列表包含要批量加载的数据库记录。在 Linux 中,它看起来类似于:
echo "this is the string being written to the DB" | sql -c "COPY table FROM stdin"
What would be the true way replacement the echo statements with a python list to be use with this command ?
我没有此过程的示例代码,因为我一直在使用一些非常简单的语法尝试 Popen 和 PIPE 的功能,但没有获得所需的结果。
任何帮助将非常感激。
谢谢
I am using python scripts to load data to a database bulk loader.
The input to the loader is stdin. I have been unable to get the correct syntax to call the unix based bulk loader passing the contents of a python list to be loaded.
I have been reading about Popen and PIPE but they have not been behaving as i expect.
The python list contains database records to be bulkloaded. In linux it would look similar to this:
echo "this is the string being written to the DB" | sql -c "COPY table FROM stdin"
What would be the correct way replace the echo statement with a python list to be used with this command ?
I do not have sample code for this process as i have been experimenting with the features of Popen and PIPE with some very simple syntax and not obtaining the desired result.
Any help would be very much appreciated.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的数据短而简单,您可以预格式化整个列表并使用 subprocess 进行简单操作,如下所示:
如果数据太大而无法像这样预格式化,那么您可以尝试直接使用 stdin 管道,尽管 subprocess 模块很不稳定使用管道时,如果您也需要从 stdout/stderr 读取数据。
If your data is short and simple, you could preformat the entire list and do it simple with subprocess like this:
If the data is too big to preformat like this, then you can attempt to use the stdin pipe directly, though subprocess module is flaky when using the pipes if you need to read from stdout/stderr too.