我有这样的日期字符串:
'January 11, 2010'
Run Code Online (Sandbox Code Playgroud)
我需要一个返回星期几的函数,比如
'mon', or 'monday'
Run Code Online (Sandbox Code Playgroud)
我在Python帮助中的任何地方都找不到这个.任何人?谢谢.
我在python中有一个协程(增强型生成器),在数据结束后要执行一些代码:
def mycoroutine():
try:
while True:
data = (yield)
print data
finally:
raise ValueError
print "END"
co = mycoroutine()
co.next()
for i in (1,2,3):
co.send(i)
Run Code Online (Sandbox Code Playgroud)
该ValueError异常没有提出,但解释简单地打印:
Exception ValueError: ValueError() in <generator object mycoroutine at 0x2b59dfa23d20> ignored
Run Code Online (Sandbox Code Playgroud)
有没有办法在调用者中捕获异常?
我在文件中有数据,我需要将其写入特定列中的CSV文件.文件中的数据如下:
002100
002077
002147
Run Code Online (Sandbox Code Playgroud)
我的代码是这样的:
import csv
f = open ("file.txt","r")
with open("watout.csv", "w") as output:
for line in f :
c.writerows(line)
Run Code Online (Sandbox Code Playgroud)
它始终写在第一列.我怎么能解决这个问题?谢谢.
我想使用一个脚本来检查是否存在目录列表,同时它应该打印一些我发送的自定义消息.
例如:
我有一个脚本验证目录是否存在:
**check.sh**
for i in $*
if [ -d "$i" ]; then
echo Found <msg-i> directory.
else
echo <msg-i> directory not found.
Run Code Online (Sandbox Code Playgroud)
现在我想这样调用这个脚本:
./check.sh $DIR1 msg1 $Dir2 msg2 $Dir3 msg3
Run Code Online (Sandbox Code Playgroud)
因此,如果DIR1不存在,那么我想将消息显示为"找不到msg1目录",类似于DIR2,我想显示"未找到msg2目录".这里msg1和msg2是我想要作为字符串传递的东西.怎么做到这一点?我正在使用bash shell.
我有一个迭代器it,我假设已经排序但我想提出异常,如果不是.
来自迭代器的数据不在内存中,所以我不想使用sorted()内置,因为AFAIK它将整个迭代器放在一个列表中.
我现在使用的解决方案是将迭代器包装在生成器函数中,如下所示:
def checkSorted(it):
prev_v = it.next()
yield prev_v
for v in it:
if v >= prev_v:
yield v
prev_v = v
else:
raise ValueError("Iterator is not sorted")
Run Code Online (Sandbox Code Playgroud)
所以我可以像这样使用它:
myconsumer(checkSorted(it))
Run Code Online (Sandbox Code Playgroud)
有人知道是否有更好的解决方案?
我知道我的解决方案有效但似乎很奇怪(至少对我而言)我自己编写一个模块来完成这样一个简单的任务.我正在寻找一个简单的单线或内置解决方案(如果存在)
在两个python列表的开头找到公共元素的最快方法是什么?我使用for循环编写它,但我认为用列表推导写它会更快...不幸的是我不知道如何在列表理解中中断.这是我写的代码:
import datetime
list1=[1,2,3,4,5,6]
list2=[1,2,4,3,5,6]
#This is the "for loop" version, and takes about 60 ms on my machine
start=datetime.datetime.now()
out=[]
for (e1, e2) in zip(list1, list2):
if e1 == e2:
out.append(e1)
else:
break
end=datetime.datetime.now()
print out
print "Execution time: %s ms" % (float((end - start).microseconds) / 1000)
#This is the list-comprehension version, it takes about 15 ms to run,
#but unfortunately returns the wrong result because I can't break the loop.
start=datetime.datetime.now()
out = [ e1 for (e1, e2) …Run Code Online (Sandbox Code Playgroud) 问题:我在solr中有一个电影信息.两个字符串字段定义电影标题和导演名称.复制字段定义solr搜索默认值的另一个字段.
我希望谷歌像搜索范围有限,如下所示.如何实现它.
1)如何搜索solr for contains
例如a)如果电影导演的名字是"John Cream",搜索joh将不会返回任何内容.但是,搜索John会返回正确的结果.
b)如果有一个名为aaabbb的电影名称和另一个名为aaa的电影标题,则搜索aaa只返回一个结果.我需要返回两个结果.
2)如何解释拼写错误
例如,如果电影导演名称为"John Cream",则搜索Jon不会返回任何结果.是否有类似(soundex)实现solr的好声音.如果是这样如何启用它?
您可以使用solr查询语法
我已经阅读过有人可以在Python中解释__all__吗?我明白它只影响from ... import *陈述,但我无法弄清楚一个真实的用例.__all__当我可以简单地避免在__init__命名空间中导入这些名称时,为什么要在(DRY!)中重复导出的名称?
例:
mypackage/__init__.py
from a import A
Run Code Online (Sandbox Code Playgroud)
mypackage/a.py
A = "A"
A1 = "A1"
Run Code Online (Sandbox Code Playgroud)
mypackage/b.py
B = "B"
Run Code Online (Sandbox Code Playgroud)
然后在python中:
>>> from mypackage import *
>>> A
'A'
>>>
>>> A1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'A1' is not defined
>>> b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'b' is not defined
Run Code Online (Sandbox Code Playgroud)
正如你所看到的A是在命名空间,但 …
我注意到,使用unirest java库cookie默认情况下是在响应中设置后发送给请求的(就像其他浏览器一样)。有什么办法可以避免呢?
例:
public class Main {
private static HttpResponse<JsonNode> doRequest() throws UnirestException {
try {
HttpResponse<JsonNode> jsonResponse = Unirest
.get("http://example.com")
.header("Accept", "application/json").asJson();
return jsonResponse;
} catch (UnirestException e) {
throw e;
}
}
public static void main(String[] args) throws UnirestException {
//first request receive a set-cookie header in response
doRequest();
//second request send a Cookie header with the cookie set by the first one: can I avoid this?
doRequest();
}
}
Run Code Online (Sandbox Code Playgroud) 如果在函数中定义类实例,则在函数退出时,实例将因范围超出而自动销毁.这可以通过一个小程序简单地验证:
class A(object):
def __del__(self):
print 'deleting A ', id(self)
class B(A):
def __init__(self):
self.a = A()
def __del__(self):
print 'deleting B ', id(self)
super(B, self).__del__()
def test():
b = B()
print 'this is ', b
test()
Run Code Online (Sandbox Code Playgroud)
输出是:
this is <__main__.B object at 0x01BC6150>
deleting B 29122896
deleting A 29122896
deleting A 29122960
Run Code Online (Sandbox Code Playgroud)
但我遇到了一个奇怪的问题.当我从novaclient继承一个类时,该实例永远不会被自动销毁.
from novaclient.v1_1.client import Client as NovaClient
class ViviNovaClient(NovaClient):
def __init__(self, auth_token, url, tenant_id):
super(ViviNovaClient, self).__init__(None, None, tenant_id, auth_url = 'http')
self.client.management_url = url
self.client.auth_token = auth_token …Run Code Online (Sandbox Code Playgroud)