Questions tagged «python»

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

16
ImportError:没有名为apiclient.discovery的模块
我在Google App Engine的Python使用过Google Translate API时遇到此错误,但是我不知道如何解决, <module> from apiclient.discovery import build ImportError: No module named apiclient.discovery 我将尝试设置指示Google App Engine SDK的环境,然后再次上传到Google Apps Engine,始终会收到错误消息, 错误:服务器错误 服务器遇到错误,无法完成您的请求。如果问题仍然存在,请报告您的问题并提及此错误消息以及引起该问题的查询。 请告诉我如何解决, 谢谢 更新:修复了在 Nijjin的帮助下,我通过添加以下文件夹修复了问题, apiclient, gflags, httplib2, oauth2client, uritemplate 如果仍然有问题,请考虑在此页面的“答案”下获取更多信息。例如 :Varum答案等...

17
在Python中重置生成器对象
我有一个由多个yield返回的生成器对象。准备调用此生成器是相当耗时的操作。这就是为什么我想多次重用生成器。 y = FunctionWithYield() for x in y: print(x) #here must be something to reset 'y' for x in y: print(x) 当然,我会考虑将内容复制到简单列表中。有没有办法重置我的发电机?
153 python  generator  yield 

5
如何通过密钥按数据组访问熊猫
如何通过密钥访问groupby对象中的相应groupby数据帧? 通过以下groupby: rand = np.random.RandomState(1) df = pd.DataFrame({'A': ['foo', 'bar'] * 3, 'B': rand.randn(6), 'C': rand.randint(0, 20, 6)}) gb = df.groupby(['A']) 我可以遍历它来获取密钥和组: In [11]: for k, gp in gb: print 'key=' + str(k) print gp key=bar A B C 1 bar -0.611756 18 3 bar -1.072969 10 5 bar -2.301539 …

5
查找和替换列表中的字符串值
我得到了这个清单: words = ['how', 'much', 'is[br]', 'the', 'fish[br]', 'no', 'really'] 我想[br]用一些与之相似的奇异值代替,<br />从而得到一个新的清单: words = ['how', 'much', 'is<br />', 'the', 'fish<br />', 'no', 'really']
153 python  string  list 

3
AttributeError:“ datetime”模块没有属性“ strptime”
这是我的Transaction课: class Transaction(object): def __init__(self, company, num, price, date, is_buy): self.company = company self.num = num self.price = price self.date = datetime.strptime(date, "%Y-%m-%d") self.is_buy = is_buy 当我尝试运行该date功能时: tr = Transaction('AAPL', 600, '2013-10-25') print tr.date 我收到以下错误: self.date = datetime.strptime(self.d, "%Y-%m-%d") AttributeError: 'module' object has no attribute 'strptime' 我该如何解决?
153 python  class  python-2.7 

14
从CSV文件创建字典?
我正在尝试从csv文件创建字典。csv文件的第一列包含唯一键,第二列包含值。csv文件的每一行代表字典中的唯一键,值对。我尝试使用csv.DictReader和csv.DictWriter类,但是只能弄清楚如何为每一行生成一个新的字典。我要一部字典。这是我尝试使用的代码: import csv with open('coors.csv', mode='r') as infile: reader = csv.reader(infile) with open('coors_new.csv', mode='w') as outfile: writer = csv.writer(outfile) for rows in reader: k = rows[0] v = rows[1] mydict = {k:v for k, v in rows} print(mydict) 当我运行上面的代码时,我得到一个ValueError: too many values to unpack (expected 2)。如何从csv文件创建一个字典?谢谢。


7
熊猫:求和给定列的DataFrame行
我有以下DataFrame: In [1]: import pandas as pd df = pd.DataFrame({'a': [1,2,3], 'b': [2,3,4], 'c':['dd','ee','ff'], 'd':[5,9,1]}) df Out [1]: a b c d 0 1 2 dd 5 1 2 3 ee 9 2 3 4 ff 1 我想增加一列'e'是列的总和'a','b'和'd'。 在各个论坛上,我认为这样会起作用: df['e'] = df[['a','b','d']].map(sum) 但事实并非如此。 我想知道适当的操作与列的列表['a','b','d']和df作为输入。
153 python  pandas  dataframe  sum 


11
在Python列表中删除重复的字典
我有一个字典列表,我想删除具有相同键和值对的字典。 对于此列表: [{'a': 123}, {'b': 123}, {'a': 123}] 我想退掉这个: [{'a': 123}, {'b': 123}] 另一个例子: 对于此列表: [{'a': 123, 'b': 1234}, {'a': 3222, 'b': 1234}, {'a': 123, 'b': 1234}] 我想退掉这个: [{'a': 123, 'b': 1234}, {'a': 3222, 'b': 1234}]
153 python  list  dictionary 




5
向后移植将Python 3 open(encoding =“ utf-8”)移植到Python 2
我有一个为Python 3构建的Python代码库,它使用带有编码参数的Python 3样式的open(): https://github.com/miohtama/vvv/blob/master/vvv/textlineplugin.py#L47 with open(fname, "rt", encoding="utf-8") as f: 现在,我想将此代码反向移植到Python 2.x,这样我将拥有一个可用于Python 2和Python 3的代码库。 建议的解决方法是什么 open()差异和缺乏编码参数的什么? 我可以使用Python 3 open()样式的文件处理程序来流字节字符串,以便像Python 2那样工作open()吗?


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.