草庐IT

try-convert

全部标签

python - 类型错误 : only length-1 arrays can be converted to Python scalars while plot showing

我有这样的Python代码:importnumpyasnpimportmatplotlib.pyplotaspltdeff(x):returnnp.int(x)x=np.arange(1,15.1,0.1)plt.plot(x,f(x))plt.show()还有这样的错误:TypeError:onlylength-1arrayscanbeconvertedtoPythonscalars我该如何解决? 最佳答案 当函数需要单个值但您传递一个数组时,会引发错误“只有长度为1的数组可以转换为Python标量”。np.int是内置int的别

python - 检查文件是否可以用 Python : try or if/else? 读取

我有以下代码:importglob,osforfileinglob.glob("\\*.txt"):ifos.access(file,os.R_OK):#Dosomethingelse:ifnotos.access(file,os.R_OK):print(file,"isnotreadable")else:print("Somethingwentwrongwithfile/dir",file)break但我不完全确定这是否是正确的做法。使用try和catch错误会更好吗?如果是这样,我该如何尝试以提高可读性?请注意我的else语句中的break。一旦无法读取文件,我就想中止循环。

python - 如何快速禁用 python 中的 try 语句进行测试?

假设我有以下代码:try:print'foo'#Alotmorecode...print'bar'except:pass出于测试目的,我如何临时禁用try-statement?你不能只注释掉try和except行,因为缩进仍然是关闭的。没有比这更简单的方法了吗?#try:print'foo'#Alotmorecode...print'bar'#except:#pass 最佳答案 您可以将异常重新引发为您的exceptblock的第一行,它的行为就像没有try/except一样。try:print'foo'#Alotmorecode.

python - "OverflowError: Python int too large to convert to C long"在 windows 但不是 mac

我在windows和mac上运行完全相同的代码,使用python3.564位。在Windows上,它看起来像这样:>>>importnumpyasnp>>>preds=np.zeros((1,3),dtype=int)>>>p=[6802256107,5017549029,3745804973]>>>preds[0]=pTraceback(mostrecentcalllast):File"",line1,inpreds[0]=pOverflowError:PythoninttoolargetoconverttoClong但是,这段代码在我的mac上运行良好。任何人都可以帮助解释原因或为

python - IOError : [Errno 2] No such file or directory trying to open a file

这个问题在这里已经有了答案:open()givesFileNotFoundError/IOError:Errno2Nosuchfileordirectory(8个回答)Whydoesn'tcallingastringmethod(suchas.replace)modify(mutate)thestring?Whydoesn'titchangeunlessIassigntheresult?(3个回答)关闭2个月前。我对Python很陌生,所以请原谅以下基本代码和问题,但我一直在试图找出导致我遇到错误的原因(我什至在S.O.上查看过类似的线程)但不能解决我的问题。这是我想要做的:循环浏览包

python - IOError : [Errno 13] Permission denied when trying to open hidden file in "w" mode

我想替换一个隐藏文件的内容,所以我尝试在w模式下打开它,这样它就会被删除/截断:>>>importos>>>ini_path='.picasa.ini'>>>os.path.exists(ini_path)True>>>os.access(ini_path,os.W_OK)True>>>ini_handle=open(ini_path,'w')但这导致了回溯:IOError:[Errno13]Permissiondenied:'.picasa.ini'但是,我能够通过r+模式达到预期的效果:>>>ini_handle=open(ini_path,'r+')>>>ini_handle.t

python - 带有空 except 代码的 Try-except 子句

这个问题在这里已经有了答案:Howtoproperlyignoreexceptions(12个回答)关闭8年前。有时您不想在except部分中放置任何代码,因为您只想确保代码运行时没有任何错误,但对捕获它们不感兴趣。我可以在C#中这样做:try{do_something()}catch(...){}我怎么能在Python中做到这一点?,因为缩进不允许这样做:try:do_something()except:i_must_enter_somecode_here()顺便说一句,也许我在C#中所做的也不符合错误处理原则。如果您对此有任何想法,我将不胜感激。 最佳答

python - Pandas ".convert_objects(convert_numeric=True)"已弃用

这个问题在这里已经有了答案:Convertpandas.Seriesfromdtypeobjecttofloat,anderrorstonans(3个回答)关闭2年前。我的代码中有这一行,它将我的数据转换为数字...data["S1Q2I"]=data["S1Q2I"].convert_objects(convert_numeric=True)问题是现在新的pandas版本(0.17.0)说这个功能已经被弃用了..这是错误:FutureWarning:convert_objectsisdeprecated.Usethedata-typespecificconverterspd.to_d

Python:try-except 作为表达式?

我发现自己一遍又一遍地有这种模式:variable=""try:variable=...dosomefileloadingstuff...except:variable=""有什么办法可以把它浓缩成一个表达式?与if-else语句一样,您可以转:variable=""ifsomething:variable=somethingelseelse:variable=""进入variable=somethingelseifsomethingelse""try-catch有什么等价的东西吗? 最佳答案 因为agf已经提供了我推荐的方法,所以

python - 标识符规范化 : Why is the micro sign converted into the Greek letter mu?

我只是偶然发现了以下奇怪的情况:>>>classTest:µ='foo'>>>Test.µ'foo'>>>getattr(Test,'µ')Traceback(mostrecentcalllast):File"",line1,ingetattr(Test,'µ')AttributeError:typeobject'Test'hasnoattribute'µ'>>>'µ'.encode(),dir(Test)[-1].encode()(b'\xc2\xb5',b'\xce\xbc')我输入的字符始终是键盘上的µ符号,但由于某种原因它被转换了。为什么会这样? 最