我必须从一些.txt文档中提取所有电子邮件地址.这些电子邮件可能具有以下格式:
a@abc.com{a, b, c}@abc.edu@迹象.我选择ruby作为我的第一语言来编写这个程序,但我不知道如何编写正则表达式.有人会帮助我吗?谢谢!
在Python 2.6.5中使用sqlite3.version 2.4.1,我使用以下代码:
c = conn.cursor()
# Create table
c.execute('''create table stocks
(date text, trans text, symbol text,
qty real, price real)''')
# Insert a row of data
c.execute("""insert into stocks
values ('2006-01-05','BUY','RHAT',100,35.14)""")
# Save (commit) the changes
conn.commit()
c.execute('''insert into stocks values(date=?, trans=?, symbol=?, qty=?, price=?
)''', ('08-26-1984', 'SELL', 'GOGL', 3, 400.00))
# We can also close the cursor
if we are done with it
c.close()
Run Code Online (Sandbox Code Playgroud)
它会抛出一个错误:
Traceback (most recent call last):
File "dbtest.py", line 18, in <module>
c.execute('''insert …Run Code Online (Sandbox Code Playgroud) 不幸的是,他们都使用PHP,这显然做了一些奇怪的事情.
我有一些服务器代码,如下所示:
@app.route("/place", methods=['GET', 'POST'])
def place():
names = request.form.getlist('name')
checks = request.form.getlist('checkboxes')
if request.form.get('Add Element'):
#return template with another form element and all the data
#return default template with N copies of the input
Run Code Online (Sandbox Code Playgroud)
现在这就是问题 - 如果我使用单选按钮作为我的是/否(选中或不选中),我就无法使用getlist,因为它们都有相同的名称.或者,我不能使用复选框,因为出于某种原因"他们"决定不应该使用"false"值发送复选框.
此解决方案不起作用:
<input id='testName' type='checkbox' value='Yes' name='testName'>
<input id='testNameHidden' type='hidden' value='No' name='testName'>
Run Code Online (Sandbox Code Playgroud)
因为,那么我想要的元素数量在1-2倍之间.
我不想使用Javascript,虽然我很容易 - 这将是一个使用率极低的网站(可能每月200-300页面浏览量,顶部,通常在月末聚集在一起).
我有一个可能的解决方案是使用"YesNo" - 但这似乎有点笨拙的一面.不幸的是,我不能轻易想到任何其他方式(不涉及javascript)来做我需要的.
我坚持select选择吗?
在一个方法中,我需要调用一些代码但是在方法的返回调用之后.我该怎么做呢?
// this call needs to happen after the return true call
xmlRpcClient.invoke("newDevices", listDeviceDesc);
return true;
Run Code Online (Sandbox Code Playgroud) 我有一行几行文字.我这样拆分了:
var lines = mytext.Split(new string[] {Environment.NewLine},
StringSplitOptions.None);
Run Code Online (Sandbox Code Playgroud)
现在,我需要获得一部分线 - 即余数.在Python中我会做这样的事情:
>>> lines = ['zero', 'one', 'two', 'three', 'four']
>>> lines[3:]
['three', 'four']
Run Code Online (Sandbox Code Playgroud)
但这显然不是.NET中的合法语法.
所以我找到了Array.Copy方法,并想通了,哈!我会这样做的:
var newlines = new string[lines.Length-someAmount]{};
Run Code Online (Sandbox Code Playgroud)
给我一个我可以复制的列表.
但后来我得到一个错误,说我不能制作这样的数组 - 这个数字必须在编译时知道!好吧,好吧,我会尝试别的东西 - 傻,但它应该有效,对吧?
var newlinesList = new List<string>(lines.Length-someAmount);
var newlines = newlinesList.ToArray();
Run Code Online (Sandbox Code Playgroud)
然后我可以使用:
Array.Copy(lines, startIndex, bodylines, 0, lines.Length-startIndex);
Run Code Online (Sandbox Code Playgroud)
除了一个问题 - 显然ToArray()过滤掉空/空值,所以我实际上无法复制它.
那么如何创建一个大小未知的数组,直到运行时,甚至更好,我怎样才能简单地切片数组呢?
我从asyncio文档稍微修改了以下代码
import asyncio
asyncio.tasks._DEBUG = True
class EchoServer(asyncio.Protocol):
def connection_made(self, transport):
name = transport.get_extra_info('sockname')
peername = transport.get_extra_info('peername')
print('Connection:',name,'<--',peername)
self.transport = transport
def data_received(self, data):
name = self.transport.get_extra_info('sockname')
peername = self.transport.get_extra_info('peername')
print('Got data:',name,'<--',peername,':',data.decode() )
if name[1] == 8888:
print("Making connection")
reader, writer = yield from asyncio.open_connection('127.0.0.1', 8889, loop=asyncio.get_event_loop())
else:
self.transport.write(data)
self.transport.close()
loop = asyncio.get_event_loop()
coro_1 = loop.create_server(EchoServer, '127.0.0.1', 8888)
coro_2 = loop.create_server(EchoServer, '127.0.0.1', 8889)
server_1 = loop.run_until_complete(coro_1)
server_2 = loop.run_until_complete(coro_2)
print('Serving on {}'.format(server_1.sockets[0].getsockname()))
print('Serving on {}'.format(server_2.sockets[0].getsockname()))
try:
loop.run_forever()
except …Run Code Online (Sandbox Code Playgroud) 我阅读了很多关于这个问题的话题,问题总是存在。当我提交登录表单时,身份验证函数()返回 None。我使用函数 set_password() 进行注册,它没有任何改变。
这是我的代码(也在这里):
from django.contrib.auth.models import User
from django.shortcuts import render
from forms import LoginForm, RegistrationForm
from django.contrib import auth
def login(request):
state = 0 # 0 = initialisation / 1 = OK / 2 = BAD_PW
form = LoginForm()
if request.method == 'POST':
error = False
if request.method == "POST":
form = LoginForm(request.POST)
if form.is_valid():
email = form.cleaned_data["email"]
password = form.cleaned_data["password"]
user = auth.authenticate(email=email,
password=password)
if user:
auth.login(request, user)
state = 1
else:
state = …Run Code Online (Sandbox Code Playgroud) 我有一些看起来非常像这样的代码:
import sqlalchemy as sa
from sqlalchemy.ext.declarative import declarative_base
engine = sa.create_engine('sqlite:///:memory:', echo=True)
Base = declarative_base()
class OneThing(Base):
__tablename__ = 'one'
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.Text)
value = sa.Column(sa.Text)
class OtherThing(Base):
__tablename__ = 'other'
id = sa.Column(sa.Integer, primary_key=True)
some_deal = sa.Column(sa.Text)
other_deal = sa.Column(sa.Text)
Base.metadata.create_all(engine)
session = sa.orm.sessionmaker(bind=engine)()
one = OneThing()
one.id = 42
one.name = 'hi'
one.value = 'roscivs'
session.add(one)
other = OtherThing()
other.id = 42
other.some_deal = 'brown'
other.other_deal = 'and sticky'
session.add(other)
session.commit()
Run Code Online (Sandbox Code Playgroud)
现在,我可以运行以下内容:
for …Run Code Online (Sandbox Code Playgroud) 我正在开发Google Chrome扩展程序.在弹出窗口中,我有以下代码:
var bookmarks = [];
function appendBMTnode(node){
bookmarks.push([node[0].title, node[0].id]);
}
function addchildren(results){
for(x = 0; x < results.length; x++){
bookmarks.push([results[x].title, results[x].id]);
chrome.bookmarks.getChildren(results[x].id, addchildren);
}
}
function getallbookmarks(){
chrome.bookmarks.get('0', appendBMTnode);
chrome.bookmarks.getChildren('0', addchildren);
}
getallbookmarks();
console.debug(bookmarks.length);
console.debug(bookmarks);
Run Code Online (Sandbox Code Playgroud)
现在,我假设第一个命令会发出我的书签数量.实际上,当我使用Chrome的调试器并将bookmarks.length添加到监视列表时,值为418.在调试器的控制台中,我可以编写bookmarks.length,它会给我正确的长度.我可以打字
for(x = 0; x < bookmarks.length; x++){ console.debug(bookmarks[x]); }
Run Code Online (Sandbox Code Playgroud)
我得到每个内部数组的字符串表示.但是,该原始console.debug(bookmarks.length)输出为零.如果我添加console.debug(bookmarks[0]);到popup.html它告诉我该值未定义.
但是,如果我添加以下内容getallbookmarks()(第一个或最后一个):
for(x = 0; x < 10; x++){
bookmarks.push(x);
}
Run Code Online (Sandbox Code Playgroud)
然后bookmarks.length首先是10,然后是428.此外,当我添加以下函数时:
function printlen(){
console.debug(bookmarks.length);
}
Run Code Online (Sandbox Code Playgroud)
如果我补充,然后在体内
<a href="#" onclick="printlen()">test</a>
Run Code Online (Sandbox Code Playgroud)
然后我也会得到正确的bookmarks.length价值.
任何线索为什么书签对象不会注册?
在java中,我的客户端类有"代码"attr和equals方法.方法等于接收另一个客户端并与其自身的代码attr进行比较.
在python中,我只是读到我们有__cmp__方法,和java方法一样做.好的,我做到了.我创建了我的类客户端,使用"code"attr和方法comp来验证代码是否相同.
class Client():
def __init__(self, code):
self.code = code
def __cmp__(self, obj):
return obj.code == self.code
def __repr__(self):
return str(self.code)
Run Code Online (Sandbox Code Playgroud)
然后我将3个Client对象放在python的列表中:
bla = [Client(1), Client(2), Client(3)]
Run Code Online (Sandbox Code Playgroud)
然后,当我尝试:
bla.remove(Client(3))
Run Code Online (Sandbox Code Playgroud)
惊人的python删除了第一个元素(客户端代码为1).
我做错了什么?我在python的Lib文件中搜索了list的实现,但是不容易找到.
有人可以帮忙吗?