所以我在Postgresql中有一个:
TAG_TABLE
==========================
id tag_name
--------------------------
1 aaa
2 bbb
3 ccc
Run Code Online (Sandbox Code Playgroud)
为了简化我的问题,当字符串"aaaaaaaa"包含'tag_name'时,我想要做的是从TAG_TABLE中选择SELECT'id'.理想情况下,它应该只返回"1",这是标记名称'aaa'的ID
这就是我到目前为止所做的事情:
SELECT id FROM TAG_TABLE WHERE 'aaaaaaaaaaa' LIKE '%tag_name%'
Run Code Online (Sandbox Code Playgroud)
但显然,这不起作用,因为postgres认为'%tag_name%'表示包含子字符串'tag_name'的模式,而不是该列下的实际数据值.
如何将tag_name传递给模式?
我正在使用Python和BeautifulSoup进行网页抓取.
让我说我有以下HTML代码来刮:
<body>
<div class="product">Product 1</div>
<div class="product">Product 2</div>
<div class="product special">Product 3</div>
<div class="product special">Product 4</div>
</body>
Run Code Online (Sandbox Code Playgroud)
使用BeautifulSoup,我想找到属性class ="product"(仅限产品1和2)的产品,而不是'特殊'产品
如果我执行以下操作:
result = soup.find_all('div', {'class': 'product'})
Run Code Online (Sandbox Code Playgroud)
结果包括所有产品(1,2,3和4).
如何找到类别与"产品"完全匹配的产品?
我运行的代码:
from bs4 import BeautifulSoup
import re
text = """
<body>
<div class="product">Product 1</div>
<div class="product">Product 2</div>
<div class="product special">Product 3</div>
<div class="product special">Product 4</div>
</body>"""
soup = BeautifulSoup(text)
result = soup.findAll(attrs={'class': re.compile(r"^product$")})
print result
Run Code Online (Sandbox Code Playgroud)
输出:
[<div class="product">Product 1</div>, <div class="product">Product 2</div>, <div class="product special">Product 3</div>, <div class="product special">Product 4</div>]
Run Code Online (Sandbox Code Playgroud) 假设我有以下Python代码:
x = some_product()
name = x.name
first_child = x.child_list[0]
link = x.link
id = x.id
Run Code Online (Sandbox Code Playgroud)
当x.child_list为None时,第3行可能会出现问题.这显然给了我一个TypeError,说:
'NoneType' Object has no attribute '_____getitem_____'
Run Code Online (Sandbox Code Playgroud)
我想要做的是,每当x.child_list [0]给出一个TypeError时,只需忽略该行并转到下一行,即" link = x.link "......
所以我猜是这样的:
try:
x = some_product()
name = x.name
first_child = x.child_list[0]
link = x.link
id = x.id
Except TypeError:
# Pass, Ignore the statement that gives exception..
Run Code Online (Sandbox Code Playgroud)
我应该在Except块下面放什么?或者还有其他方法可以做到这一点吗?
我知道我可以使用如果x.child_list不是None:...,但我的实际代码要复杂得多,我想知道是否有更多的pythonic方法来做到这一点
我有一个用于“人”模型的ImageField。
from django.db import models
class People(models.Model):
firstname = models.CharField(max_length=100)
lastname = models.CharField(max_length=100)
img = models.ImageField(upload_to='media/people')
Run Code Online (Sandbox Code Playgroud)
问题是,当我尝试通过Django Admin添加人员对象并选择图像文件时,Django admin在图像的末尾添加了奇怪的字符(看起来像哈希值),这导致图像不显示在网站上。
例如,这是我的Django Admin页面,用于添加人员:

当我单击保存并检查我的管理员时,将显示以下内容:

如您所见,由于某种原因,它在图像名称的末尾添加了“ _PQSSbcg”。因此,网站无法显示图像,因为模板试图找到“ jose_atria.jpg”。
Django Admin为什么要添加这些额外的字符,我该如何摆脱呢?
谢谢
我正在尝试找到一个Python方法/库来测试自变量X和二进制输出Y之间的相关性.
例如,假设我有以下数据和输出:
X Y
0.65 1
0.11 0
0.13 0
0.35 1
0.21 0
...
假设输出Y如果(X> 0.3)则为1,否则为0.如果我不知道这种相关性(阈值0.3),是否有统计方法/测试来找出X和Y之间的相关程度?
例如,一些返回的方法
x = [0.65, 0.11, 0.13, 0.31, 0.21]
y = [1, 0, 0, 1, 0]
print some_test(x, y)
==> returns "degree of correlation = 1.0"
Run Code Online (Sandbox Code Playgroud)
谢谢
(与SQL Server相关的类似问题:SO Link)
我知道在Sql Server 2008或更高版本中,您可以插入多行执行以下操作:
INSERT INTO MyTable (Name, ID)
VALUES ('First',1), ('Second',2), ('Third',3)
Run Code Online (Sandbox Code Playgroud)
但是,似乎此语法在Sybase Adaptive Server Enterprise中不起作用,因为这会给我一个错误.
任何人都知道Sybase中的语法实现了同样的目的吗?
Sybase ASE基于Transact SQL ..
谢谢
我正在使用 Navicat 连接到在我的 Heroku 应用程序上运行的 Postgres。我输入了正确的主机、端口、数据库名称、用户、密码,但出现以下连接错误:

我搜索了此错误的解决方案,并通过添加以下行修改了pg_hba.conf:
host all all 0.0.0.0/0 md5
Run Code Online (Sandbox Code Playgroud)
我还取消了 postgres.conf 中以下几行的注释:
listen_address = '*'
port = 5432 # (change requires restart)
max_connections = 100 # (change requires restart)
Run Code Online (Sandbox Code Playgroud)
我重新启动了 Postgres,但 Navicat 仍然给我同样的错误。
我认为问题可能出在 SSL 设置上,所以我遵循了本手册:http : //www.navicat.com/manual/online_manual/en/navicat_datamodeler/win_manual/SSLCert.html
尽管如此,我还是遇到了同样的错误。
任何人都可以假设问题是什么?
以下是别人的c++教程中的自定义异常类:
class MyException : public std::exception {
public:
MyException(const std::string message) : m_message(message) { }
~MyException() throw() { }
const std::string what() const throw() { return m_message.c_str(); }
private:
std::string m_message;
}
Run Code Online (Sandbox Code Playgroud)
我得到了这里发生的大部分事情,除了析构函数旁边的“throw()”。
这是否意味着只要异常类被破坏就调用 throw() ..??
如果我有一个Base和一个派生类:
class Base {
//...
};
class Derived : public Base {
//...
};
Run Code Online (Sandbox Code Playgroud)
是否可以通过以下方式重载功能?
void DoSomething(Base b) {
cout << "Do Something to Base" << endl;
}
void DoSomething(Derived d) {
cout << "Do Something to Derived" << endl;
}
Run Code Online (Sandbox Code Playgroud)
如果我这样做会发生什么:
int main() {
Derived d = Derived();
DoSomething(d);
}
Run Code Online (Sandbox Code Playgroud)
Derived也是一个Base ..所以调用哪个版本?
python ×4
c++ ×2
database ×2
exception ×2
postgresql ×2
sql ×2
correlation ×1
django ×1
heroku ×1
html ×1
inheritance ×1
navicat ×1
optimization ×1
overloading ×1
polymorphism ×1
regex ×1
sql-server ×1
ssl ×1
sybase ×1
t-sql ×1
web-scraping ×1