草庐IT

print_help

全部标签

python `print` 在循环中不起作用

我有多个循环在一起,sleep在最内层的循环中。例如:fromtimeimportsleepforiinrange(10):printi,forjinrange(-5,5):ifj>0:print'.',else:print'D',sleep(1)print''如果你运行代码,你可能希望在它Dsleep1秒之后得到i值,然后另一个D再次sleep直到到最后。但结果不同,它等待10秒并打印整行0DDDDDDD。...并再次等待打印下一行。我发现打印末尾的逗号导致了这个问题。我该如何解决? 最佳答案 由于逗号的存在,输出缓冲到\n。您应

python pdb自动 pretty-print

我发现自己经常在pdb中这样做:importpprintpprint.PrettyPrinter().pprint(variable_of_interest)是否有更好的方法从pdb中漂亮地打印变量?我正在寻找更容易输入的东西,理想情况下是pdb中始终可用的东西,这样我可以在调试时随时使用它。 最佳答案 在pdb文档的DebuggerCommands部分:ppexpressionLikethepcommand,exceptthevalueoftheexpressionispretty-printedusingthepprintmod

python - 操作系统错误 : raw write() returned invalid length when using print() in python

我正在使用pythontensorflow训练一个模型来识别python中的图像。但是当我尝试从github执行train.py时出现以下错误Traceback(mostrecentcalllast):File"train.py",line1023,intf.app.run(main=main,argv=[sys.argv[0]]+unparsed)File"C:\Users\sande\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\platform\app.py",line48,inrun_sys.exit

print obj 和 print obj.__str__() 之间的 Python 区别 [至少对于 Unicode?]

我被告知调用printobj将调用obj.__str__(),后者将返回一个字符串以打印到控制台。现在我遇到了一个Unicode问题,我无法打印任何非ascii字符。我得到了典型的“ascii超出范围”的东西。在尝试以下工作时:printobj.__str__()printobj.__repr__()两个函数执行完全相同的操作(__str__()只是返回self.__repr__())。什么不起作用:printobj只有在使用超出ascii范围的字符时才会出现问题。最终的解决方案是在__str__()中执行以下操作:returnself.__repr__().encode(sys.st

python - 为什么 tf.Print() 不在 tensorflow 中打印

下面的代码没有给出任何错误,但也没有打印张量。importtensorflowastfimportnumpyasnp#Sometensorwewanttoprintthevalueofx=tf.placeholder(tf.float32,shape=[2,2,2])a=np.array([[[1.,1.],[1.,1.]],[[2.,2.],[2.,2.]]])m=tf.Print(x,[x])withtf.Session()assess:sess.run(tf.initialize_all_variables())m_eval=m.eval(session=sess,feed_di

Python 参数解析 : Insert blank line between help entries

使用argparse时,将--help传递给程序会生成帮助文本。不幸的是,它很难阅读,因为选项之间没有空行。摘录如下:optionalarguments:-h,--helpshowthishelpmessageandexit-uFILENAME,--up-soundFILENAMEThesoundtoplaywhenthenetworkcomesup.Default:"/path/to/some/sound/file.wav"-dFILENAME,--down-soundFILENAMEThesoundtoplaywhenthenetworkgoesdown.Default:"/pat

python - 将 Unicode 与字符串 : print '£' + '1' works, 连接但打印 '£' + u'1' 会抛出 UnicodeDecodeError

我观察到以下情况:>>>print'£'+'1'£1>>>print'£'+u'1'Traceback(mostrecentcalllast):File"",line1,inUnicodeDecodeError:'ascii'codeccan'tdecodebyte0xc2inposition0:ordinalnotinrange(128)>>>printu'£'+u'1'£1>>>printu'£'+'1'£1为什么'£'+'1'有效而'£'+u'1'无效?我查看了类型:>>>type('£'+'1')>>>type('£'+u'1')Traceback(mostrecentcall

python - 新的 jupyter notebook 中的 sympy pretty printing 坏了吗?

我以前在ipython笔记本中使用过漂亮的数学打印。升级到jupyter后(也升级了许多其他ipython相关的包),pretty-print不再像以前那样工作。我在笔记本顶部使用这段代码进行设置:importsympyasspsp.init_printing()我也尝试过将use_latex=True和use_latex='mathjax'参数添加到init_printing,但这并没有帮助。在所有情况下,表达式在升级后都以纯文本形式打印。参见https://gist.github.com/josteinbf/78dae5085dec0aa19a48#file-sympy_pp-ip

python - python 2.7 中的 print 和 print() 有什么区别

我是Python新手。我在python2.7上运行以下代码,当我使用print或print()时看到不同的结果。这两个函数有什么区别?我读了其他问题,例如thisquestion,但我没有找到答案。classRectangle:def__init__(self,w,h):self.width=wself.height=hdef__str__(self):return"(Thewidthis:{0},andtheheightis:{1})".format(self.width,self.height)box=Rectangle(100,200)print("box:",box)print

python - python print() 函数实际上做了什么?

我在看这个question并开始想知道print是什么意思实际上做。我一直不知道如何使用string.decode()和string.encode()在python交互式shell中以与打印相同的格式获取unicode字符串“out”。无论我做什么,我都会得到UnicodeEncodeError或带有“\x##”符号的转义字符串...这是python2.x,但我已经在尝试改正我的方法并实际调用print():)例子:>>>importsys>>>a='\xAA\xBB\xCC'>>>print(a)ª»Ì>>>a.encode(sys.stdout.encoding)Traceback