Questions tagged «python»

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


7
尝试合并2个数据框但出现ValueError
这是我保存在两个变量中的两个数据框: > print(df.head()) > club_name tr_jan tr_dec year 0 ADO Den Haag 1368 1422 2010 1 ADO Den Haag 1455 1477 2011 2 ADO Den Haag 1461 1443 2012 3 ADO Den Haag 1437 1383 2013 4 ADO Den Haag 1386 1422 2014 > print(rankingdf.head()) > club_name ranking year 0 …

4
如何在PyCharm中设置环境变量?
我已经开始在Django项目中工作,我想设置一些环境变量,而不必手动设置它们或将bash文件作为源。 我想设置以下变量: export DATABASE_URL=postgres://127.0.0.1:5432/my_db_name export DEBUG=1 # there are other variables, but they contain personal information 我已经读过这篇文章,但这并不能解决我想要的问题。另外,我尝试在Preferences- > Build,Execution,Deployment- > Console- > Python Console / Django Console中设置环境变量,但是它设置了解释器的变量。

24
使用Python获取机器的外部IP地址
寻找一种使计算机具有当前外部IP的更好方法...可以正常工作,但宁愿不依赖外部站点来收集信息...我仅限于使用与Mac OS捆绑在一起的标准Python 2.5.1库X 10.5.x import os import urllib2 def check_in(): fqn = os.uname()[1] ext_ip = urllib2.urlopen('http://whatismyip.org').read() print ("Asset: %s " % fqn, "Checking in from IP#: %s " % ext_ip)

2
list()比列表理解使用更多的内存
因此,我在玩list对象时发现一点奇怪的事情:如果list用list()它创建的东西比列表理解力要占用更多的内存?我正在使用Python 3.5.2 In [1]: import sys In [2]: a = list(range(100)) In [3]: sys.getsizeof(a) Out[3]: 1008 In [4]: b = [i for i in range(100)] In [5]: sys.getsizeof(b) Out[5]: 912 In [6]: type(a) == type(b) Out[6]: True In [7]: a == b Out[7]: True In [8]: sys.getsizeof(list(b)) Out[8]: 1008 从文档: …

8
使用熊猫read_csv时出现内存错误
我正在尝试做相当简单的事情,将一个大的csv文件读入pandas数据框。 data = pandas.read_csv(filepath, header = 0, sep = DELIMITER,skiprows = 2) 代码要么以失败MemoryError,要么就永远无法完成。 任务管理器中的内存使用停止在506 Mb,并且在5分钟内没有更改并且在该过程中没有CPU活动之后,我将其停止。 我正在使用pandas版本0.11.0。 我知道文件解析器曾经存在内存问题,但是根据http://wesmckinney.com/blog/?p=543,该问题应该已得到解决。 我试图读取的文件是366 Mb,如果我将文件切成短片(25 Mb),则上面的代码将起作用。 还发生了一个弹出窗口,告诉我它无法写入地址0x1e0baf93 ... 堆栈跟踪: Traceback (most recent call last): File "F:\QA ALM\Python\new WIM data\new WIM data\new_WIM_data.py", line 25, in <module> wimdata = pandas.read_csv(filepath, header = 0, sep = DELIMITER,skiprows = 2 …
79 python  windows  pandas 


11
随机根本就不是随机的吗?
我这样做是为了测试randint的随机性: >>> from random import randint >>> >>> uniques = [] >>> for i in range(4500): # You can see I was optimistic. ... x = randint(500, 5000) ... if x in uniques: ... raise Exception('We duped %d at iteration number %d' % (x, i)) ... uniques.append(x) ... Traceback (most …

5
检查多个变量是否不为None的最有效方法是什么?
如果我有这样的构造: def foo(): a=None b=None c=None #...loop over a config file or command line options... if a is not None and b is not None and c is not None: doSomething(a,b,c) else: print "A config parameter is missing..." python中检查所有变量是否都设置为有用值的首选语法是什么?是我写的还是其他更好的方法? 这与以下问题不同: 在Python中不是None测试...我正在寻找检查许多条件是否为None的首选方法。我输入的选项似乎很长,而且不是Python语言。
79 python 

2
在python中将json转换为字符串
一开始我没有清楚地解释我的问题。在JSON中将json转换为字符串时,请尝试使用str()和json.dumps()。 >>> data = {'jsonKey': 'jsonValue',"title": "hello world"} >>> print json.dumps(data) {"jsonKey": "jsonValue", "title": "hello world"} >>> print str(data) {'jsonKey': 'jsonValue', 'title': 'hello world'} >>> json.dumps(data) '{"jsonKey": "jsonValue", "title": "hello world"}' >>> str(data) "{'jsonKey': 'jsonValue', 'title': 'hello world'}" 我的问题是: >>> data = {'jsonKey': 'jsonValue',"title": "hello world'"} >>> str(data) '{\'jsonKey\': \'jsonValue\', …
79 python  json  string 

10
如何在Python中读取HDF5文件
我正在尝试从Python中的hdf5文件读取数据。我可以使用读取hdf5文件h5py,但无法弄清楚如何访问文件中的数据。 我的密码 import h5py import numpy as np f1 = h5py.File(file_name,'r+') 这可以正常工作并读取文件。但是,如何访问文件对象内部的数据f1?
79 python  hdf5 

10
如何升级pip3?
我想使用python3.5进行基本开发,但是很多时候我为python 3.5安装模块时,它总是失败。终端告诉我,有更高版本可用,升级时它不起作用。

12
如何通过脚本将标准输出重定向到文件和控制台?
我想运行一个python脚本并捕获文本文件上的输出以及想在控制台上显示。 我想将其指定为python脚本本身的属性。不要echo "hello world" | tee test.txt每次都在命令提示符下使用该命令。 在脚本中,我尝试过: sys.stdout = open('log.txt','w') 但这不会在屏幕上显示stdout输出。 我听说过有关日志记录模块的信息,但是使用该模块完成这项工作我不走运。

3
防止熊猫将'NA'解释为字符串中的NaN
熊猫的read_csv()方法将'NA'解释为nan(不是数字),而不是有效的字符串。 在下面的简单情况下,请注意,第1行第2列(基于零的计数)的输出为'nan'而不是'NA'。 sample.tsv(制表符分隔) PDB链SP_PRIMARY RES_BEG RES_END PDB_BEG PDB_EN​​D SP_BEG SP_END 5d8b N P60490 1146 1146 1146 5d8b NA P80377 1 126 1126 1126 5d8b O P60491 1 118 1 118 1 118 read_sample.py import pandas as pd df = pd.read_csv( 'sample.tsv', sep='\t', encoding='utf-8', ) for df_tuples in df.itertuples(index=True): print(df_tuples) 输出 …
79 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.