pycharm:从“调试”中的标准输入读取模式 [英] pycharm: read from standard input in "debug" mode

查看:988
本文介绍了pycharm:从“调试”中的标准输入读取模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PyCharm中调试这个脚本:

  import sys 

def read_in() :
lines = sys.stdin.readlines()
for i in range(len(lines)):
lines [i] = lines [i] .replace('\\\
' ,'')
返回行

def main():
lines = read_in()
打印行

如果__name__ == '__main__':
main()

我通常使用如下命令来调用此脚本: code> cat data.txt | python script.py 将data.txt提交到标准输入。



我的问题是如何在运行/调试配置 PyCharm以相同的方式将data.txt提供到标准输入,但是哪些可以让我使用PyCharm的真棒调试模式?我怀疑我需要正确填写脚本参数字段,但是到目前为止我已经把我拖过来了。



谢谢。

解决方案

个人而言,用一个Mock替换sys.stdin.readlines(),并返回你想要的值。将其放入单元测试中,并运行单元测试。



另请参阅
从Pycharm中的sys.stdin文件读取


I'm debugging this script in PyCharm:

import sys

def read_in():
    lines = sys.stdin.readlines()
    for i in range(len(lines)):
        lines[i] = lines[i].replace('\n','')
    return lines

def main():
    lines = read_in()
    print lines

if __name__ == '__main__':
    main()

I normally call this script using a command like cat data.txt | python script.py which feeds data.txt into Standard Input.

My question is how can I setup a "Run/Debug Configuration" in PyCharm which feeds data.txt into Standard Input in the same way, but which will allow me to use PyCharm's awesome debugging mode? I suspect that I need to correctly fill out the "Script Parameters" field -- but this has eluded me so far.

Thanks.

解决方案

Personally, I'd just use patch to replace sys.stdin.readlines() with a Mock, with the return value you desire. Place that into a unit test, and run the unit test.

Also, see Reading from a file with sys.stdin in Pycharm

这篇关于pycharm:从“调试”中的标准输入读取模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆