Questions tagged «python»

Python是一种多范式,动态类型的多用途编程语言。它旨在快速学习,理解和使用并强制使用干净统一的语法。请注意,Python 2自2020年1月1日起已不再受支持。不过,对于特定于版本的Python问题,请添加[python-2.7]或[python-3.x]标签。使用Python变体或库(例如Jython,PyPy,Pandas,Numpy)时,请将其包含在标签中。

14
使用Python在SSH上执行命令
我正在编写一个脚本来自动化Python中的某些命令行命令。目前,我正在打电话: cmd = "some unix command" retcode = subprocess.call(cmd,shell=True) 但是我需要在远程计算机上运行一些命令。手动地,我将使用ssh登录,然后运行命令。我将如何在Python中自动执行此操作?我需要使用(已知)密码登录到远程计算机,所以我不能只使用cmd = ssh user@remotehost,我想知道是否应该使用一个模块?
136 python  ssh 

13
Python速度测试-时差-毫秒
为了快速测试一段代码,在Python中进行两次比较的正确方法是什么?我尝试阅读API文档。我不确定我是否了解timedelta。 到目前为止,我有以下代码: from datetime import datetime tstart = datetime.now() print t1 # code to speed test tend = datetime.now() print t2 # what am I missing? # I'd like to print the time diff here

7
如何将NumPy数组标准化到一定范围内?
在对音频或图像阵列进行一些处理之后,需要先在一定范围内对其进行标准化,然后才能将其写回到文件中。可以这样完成: # Normalize audio channels to between -1.0 and +1.0 audio[:,0] = audio[:,0]/abs(audio[:,0]).max() audio[:,1] = audio[:,1]/abs(audio[:,1]).max() # Normalize image to between 0 and 255 image = image/(image.max()/255.0) 有没有那么繁琐,方便的函数方式来做到这一点?matplotlib.colors.Normalize()似乎无关。


4
NameError:全局名称“ unicode”未定义-在Python 3中
我正在尝试使用一个名为bidi的Python包。在此程序包(algorithm.py)的模块中,尽管它是程序包的一部分,但仍有一些行会给我带来错误。 以下是这些行: # utf-8 ? we need unicode if isinstance(unicode_or_str, unicode): text = unicode_or_str decoded = False else: text = unicode_or_str.decode(encoding) decoded = True 这是错误消息: Traceback (most recent call last): File "<pyshell#25>", line 1, in <module> bidi_text = get_display(reshaped_text) File "C:\Python33\lib\site-packages\python_bidi-0.3.4-py3.3.egg\bidi\algorithm.py", line 602, in get_display if isinstance(unicode_or_str, unicode): NameError: global …


5
如何更新SQLAlchemy行条目?
假设表有三列:username,password和no_of_logins。 当用户尝试登录时,将使用查询之类的内容检查条目 user = User.query.filter_by(username=form.username.data).first() 如果密码匹配,他将继续。我想做的是计算用户登录的次数。因此,无论他何时成功登录,我都希望增加该no_of_logins字段并将其存储回用户表。我不确定如何使用SqlAlchemy运行更新查询。


4
熊猫加入问题:列重叠但未指定后缀
我有以下2个数据帧: df_a = mukey DI PI 0 100000 35 14 1 1000005 44 14 2 1000006 44 14 3 1000007 43 13 4 1000008 43 13 df_b = mukey niccdcd 0 190236 4 1 190237 6 2 190238 7 3 190239 4 4 190240 7 当我尝试加入这两个数据框时: join_df = df_a.join(df_b,on='mukey',how='left') 我得到错误: …
136 python  join  pandas 

6
如何将tsv文件加载到Pandas DataFrame中?
我是python和pandas的新手。我正在尝试将tsv文件加载到熊猫中DataFrame。 这是我正在尝试的错误: >>> df1 = DataFrame(csv.reader(open('c:/~/trainSetRel3.txt'), delimiter='\t')) Traceback (most recent call last): File "<pyshell#28>", line 1, in <module> df1 = DataFrame(csv.reader(open('c:/~/trainSetRel3.txt'), delimiter='\t')) File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 318, in __init__ raise PandasError('DataFrame constructor not properly called!') PandasError: DataFrame constructor not properly called!
136 python  pandas  csv 


8
熊猫将列表的一列分为多列
我有一列的pandas DataFrame: import pandas as pd df = pd.DataFrame( data={ "teams": [ ["SF", "NYG"], ["SF", "NYG"], ["SF", "NYG"], ["SF", "NYG"], ["SF", "NYG"], ["SF", "NYG"], ["SF", "NYG"], ] } ) print(df) 输出: teams 0 [SF, NYG] 1 [SF, NYG] 2 [SF, NYG] 3 [SF, NYG] 4 [SF, NYG] 5 [SF, NYG] …
136 python  pandas 




By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.