Questions tagged «python»

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

3
附加到熊猫中的空DataFrame吗?
是否可以将其追加到不包含任何索引或列的空数据框中? 我试图这样做,但最后总是得到一个空的数据框。 例如 df = pd.DataFrame() data = ['some kind of data here' --> I have checked the type already, and it is a dataframe] df.append(data) 结果看起来像这样: Empty DataFrame Columns: [] Index: []
212 python  pandas 



13
如何使Flask在端口80上运行?
我有一个通过端口5000运行的Flask服务器,很好。我可以在http://example.com:5000上访问它 但是是否可以在http://example.com上简单地访问它?我假设这意味着我必须将端口从5000更改为80。但是当我在Flask上尝试该端口时,运行该错误消息。 Traceback (most recent call last): File "xxxxxx.py", line 31, in <module> app.run(host="0.0.0.0", port=int("80"), debug=True) File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 772, in run run_simple(host, port, self, **options) File "/usr/local/lib/python2.6/dist-packages/werkzeug/serving.py", line 706, in run_simple test_socket.bind((hostname, port)) File "<string>", line 1, in bind socket.error: [Errno 98] Address already in use 连续lsof -i :80收益 …
212 python  networking  flask  port 



16
如何检查Python中的字符串是否为ASCII?
我想检查字符串是否为ASCII。 我知道ord(),但是当我尝试时ord('é'),我知道了TypeError: ord() expected a character, but string of length 2 found。我了解这是由我构建Python的方式引起的(如ord()的文档中所述)。 还有另一种检查方法吗?
211 python  string  unicode  ascii 

4
为什么Pylint认为在条件值中使用len(SEQUENCE)不正确?
考虑以下代码片段: from os import walk files = [] for (dirpath, _, filenames) in walk(mydir): # more code that modifies files if len(files) == 0: # <-- C1801 return None Pylint使我对有关if语句行的消息感到震惊: [pylint] C1801:请勿len(SEQUENCE)用作条件值 乍一看,规则C1801在我看来并不十分合理,参考指南中的定义也无法解释为什么这是一个问题。实际上,它彻头彻尾地称其为不正确的用法。 len-as-condition(C1801): 不要len(SEQUENCE)用作条件值当Pylint检测到内部条件不正确使用len(sequence)时使用。 我的搜索尝试也未能为我提供更深入的解释。我确实知道,序列的length属性可能会被延迟评估,并且__len__可以编程为具有副作用,但是令人怀疑的是,仅此一个问题是否足以使Pylint认为这种用法不正确。因此,在我简单地将项目配置为忽略规则之前,我想知道我的推理中是否缺少某些内容。 什么时候将len(SEQ)用作条件值有问题?Pylint尝试使用C1801避免哪些主要情况?


13
如何将int转换为十六进制字符串?
我想将一个整数(将为<= 255)用于十六进制字符串表示形式 例如:我想通过65并离开'\x41',或255获得'\xff'。 我曾尝试使用struct.pack('c',65来执行此操作),但9由于它想采用单个字符串,因此上述内容均会阻塞。
210 python  string  hex  int 




4
如何打开文件进行读写?
有没有办法打开文件进行读写? 解决方法是,打开文件进行写入,将其关闭,然后再次打开以进行读取。但是,有没有办法打开一个文件都阅读和写作?
210 python  file  file-io 

14
如何使用Python将文件的整个目录复制到现有目录中?
从包含一个名为目录bar(包含一个或多个文件)和一个目录baz(也包含一个或多个文件)的目录中运行以下代码。确保没有名为的目录foo。 import shutil shutil.copytree('bar', 'foo') shutil.copytree('baz', 'foo') 它将失败并显示: $ python copytree_test.py Traceback (most recent call last): File "copytree_test.py", line 5, in <module> shutil.copytree('baz', 'foo') File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/shutil.py", line 110, in copytree File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/os.py", line 172, in makedirs OSError: [Errno 17] File exists: 'foo' 我希望它的工作方式与输入相同: $ mkdir foo $ cp bar/* foo/ …
210 python  shutil  copytree 

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.