假设我有一个如此定义的结构:
cdef extern from "blah.h":
struct my_struct:
int a
int b
Run Code Online (Sandbox Code Playgroud)
我需要能够将转换dict成my_struct,而不承担my_struct的领域的知识.换句话说,我需要进行以下转换:
def do_something(dict_arg):
cdef my_struct s = dict_arg
do_somthing_with_s(s)
Run Code Online (Sandbox Code Playgroud)
问题是Cython不会这样做:http://docs.cython.org/src/userguide/language_basics.html#automatic-type-conversions
当然,如果我对这个my_struct领域的名字有所了解,我可以这样做:
def do_something(dict_arg):
cdef my_struct s = my_struct(a=dict_arg['a'], b=dict_arg['b'])
do_somthing_with_s(s)
Run Code Online (Sandbox Code Playgroud)
这样做会使cython编译器崩溃:
def do_something(dict_arg):
cdef my_struct s = my_struct(**dict_arg)
do_somthing_with_s(s)
Run Code Online (Sandbox Code Playgroud)
我不知道该字段名称的原因是因为代码是自动生成的,我不想做一个丑陋的黑客来处理这种情况.
如何使用Cython从Python dict初始化结构?
我想分析类方法的时间和内存使用情况.我没有找到这一个彻头彻尾的现成的解决方案(有没有这样的模块?),我决定使用timeit时间分析和memory_usage从memory_profiler模块.
我遇到了分析方法的问题memory_profiler.我尝试了不同的变体,但没有一个变得有效.
当我尝试使用partial from时functools,我收到此错误:
File "/usr/lib/python2.7/site-packages/memory_profiler.py", line 126, in memory_usage
aspec = inspect.getargspec(f)
File "/usr/lib64/python2.7/inspect.py", line 815, in getargspec
raise TypeError('{!r} is not a Python function'.format(func))
TypeError: <functools.partial object at 0x252da48> is not a Python function
Run Code Online (Sandbox Code Playgroud)
顺便说一下,完全相同的方法与timeit函数一起工作.
当我尝试使用时lambda,我收到此错误:
File "/usr/lib/python2.7/site-packages/memory_profiler.py", line 141, in memory_usage
ret = parent_conn.recv()
IOError: [Errno 4] Interrupted system call
Run Code Online (Sandbox Code Playgroud)
如何使用memory_profiler处理类方法?
PS:我有内存分析器(0.26)(用pip安装).
UPD:实际上是bug.你可以在这里查看状态:https://github.com/fabianp/memory_profiler/issues/47
我使用的是Python 2.7和MySQLdb 1.2.3.我尝试了在stackoverflow和其他论坛上找到的所有内容来处理我的脚本抛出的编码错误.我的脚本从源MySQL DB中的所有表读取数据,将它们写入python StringIO.StringIO对象,然后将该数据从StringIO对象加载到Postgres数据库(显然是UTF-8编码格式.我通过查看属性找到了这个 -使用psycopg2库的copy_from命令在pgadmin中定义数据库.
我发现我的源MySQL数据库在latin1_swedish_ci编码中有一些表,而其他表用utf_8编码格式(在information_schema.tables中找到这个来自TABLE_COLLATION).
我根据我在互联网上的研究,在Python脚本的顶部编写了所有这些代码.
db_conn = MySQLdb.connect(host=host,user=user,passwd=passwd,db=db, charset="utf8", init_command='SET NAMES UTF8' ,use_unicode=True)
db_conn.set_character_set('utf8')
db_conn_cursor = db_conn.cursor()
db_conn_cursor.execute('SET NAMES utf8;')
db_conn_cursor.execute('SET CHARACTER SET utf8;')
db_conn_cursor.execute('SET character_set_connection=utf8;')
Run Code Online (Sandbox Code Playgroud)
我仍然得到了UnicodeEncodeError下面这一行:cell = str(cell).replace("\r", " ").replace("\n", " ").replace("\t", '').replace("\"", "") #Remove unwanted characters from column value,
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 47: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
在写入StringIO对象时,我编写了以下代码行来清理源MySQL数据库的每个表中的单元格.
cell = str(cell).replace("\r", " ").replace("\n", " ").replace("\t", '').replace("\"", "") #Remove unwanted characters from column …Run Code Online (Sandbox Code Playgroud) 我试图获得一个英语单词的基本英语单词,该单词是从其基本形式修改的.这个问题已在这里提出,但我没有看到正确的答案,所以我试图这样说.我尝试了两个来自NLTK包的词干器和一个词形变换器,它们是搬运器,干扰器,雪球器和wordnet lemmatiser.
我试过这段代码:
from nltk.stem.porter import PorterStemmer
from nltk.stem.snowball import SnowballStemmer
from nltk.stem.wordnet import WordNetLemmatizer
words = ['arrival','conclusion','ate']
for word in words:
print "\n\nOriginal Word =>", word
print "porter stemmer=>", PorterStemmer().stem(word)
snowball_stemmer = SnowballStemmer("english")
print "snowball stemmer=>", snowball_stemmer.stem(word)
print "WordNet Lemmatizer=>", WordNetLemmatizer().lemmatize(word)
Run Code Online (Sandbox Code Playgroud)
这是我得到的输出:
Original Word => arrival
porter stemmer=> arriv
snowball stemmer=> arriv
WordNet Lemmatizer=> arrival
Original Word => conclusion
porter stemmer=> conclus
snowball stemmer=> conclus
WordNet Lemmatizer=> conclusion
Original Word => ate
porter stemmer=> ate
snowball stemmer=> ate
WordNet …Run Code Online (Sandbox Code Playgroud) 最终我只是想获得第一个链接到谷歌搜索结果的href
我需要的信息也存在于“a”元素中,但它存储在“data-href”属性中,我无法弄清楚如何从中提取数据(get_attribute('data-href')返回None)。
我正在使用 Phantomjs,但也尝试过使用 Firefox 网络驱动程序
href 显示在cite谷歌搜索的标签中(可以通过检查谷歌搜索结果中每个链接下的绿色小链接文本找到)。
引用元素显然是在 Selenium 中找到的,但返回的文本 ( element.text, or get_attribute('innerHTML'), or ( text)) 不是 html 中显示的内容。
例如,有一个 cite 标签<cite class="_Rm">www.fcv.org.br/</cite>,但element.text显示“wikimapia.org/.../Fundação-Cristiano-Varella-Hospital...”
我试图用, , 和 xpath检索 cite 元素by_css_selector,结果相同。tag_nameclass_name
links = driver.find_elements_by_css_selector('div.g') # div[class="g"]
link = links[0] # I am looking for the first link in the main links section
next = link.find_element_by_css_selector('div[class="s"]') # location of cite tag
nextB = next.find_element_by_tag_name('cite') …Run Code Online (Sandbox Code Playgroud) 我想使用 Python 连接到 cPanel 的数据库内部并插入一些新数据。我可以编写 SQL 和 Python 代码来进行插入。但是,不知道如何连接cPanel。
我在 cPanel 的 phpMyAdmin 中有网站地址(比如 www.myuscanadahouse.com)、cPanel 用户名、cPanel 密码和数据库名称。Python 中的一些教程或启动代码会有所帮助。我看过这个页面https://pypi.python.org/pypi/pycpanel/0.1.2#id6,但我无法在这里工作。
连接 MySQL 数据库时出现以下错误:
_mysql_exceptions.OperationalError: (1045, "Access denied for user'buildersa'@'47.124.85.71' (using password: YES)")
Run Code Online (Sandbox Code Playgroud) 我有一个页面用ajax加载动态内容,然后在一定时间后重定向(不固定).如何强制Selenium Webdriver等待页面重定向,然后立即转到另一个链接?
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Chrome();
driver.get("http://www.website.com/wait.php")
Run Code Online (Sandbox Code Playgroud) 我正在尝试按照本教程使用 pyx、pxd 和 cimport 创建和使用扩展类型。
在终端中编译 Cython 文件时,我收到一个我不知道如何纠正的错误:
cdef class CythonClass: 在 pyx 文件中被指示为错误行。
File "/Library/Python/2.7/site-packages/Cython/Build/Dependencies.py", line 1056, in cythonize_one
raise CompileError(None, pyx_file)
Cython.Compiler.Errors.CompileError: CythonClass.pyx
Run Code Online (Sandbox Code Playgroud)
我pip install cython在 MacOS Sierra 上使用 Cython 版本 .25(并且也尝试过其他版本,每个版本都安装了)。Python 版本是 2.7.10。
按照建议,我安装了 gcc(Xcode 8.2 的命令行工具),但仍然收到错误消息。
文件内容:
像素:
cdef class CythonClass:
cdef:
list list1
dict d1, d2, d3, d4, d5, d6
Run Code Online (Sandbox Code Playgroud)
pyx:
cdef class CythonClass:
def __init__(self):
self.list1 = []
self.d1 = {}
self.d2 = {}
self.d3 = {}
self.d4 = {} …Run Code Online (Sandbox Code Playgroud) cursor.execute(…)在执行之前多次执行有效的 PyMySQL 操作connection.commit(),还是connection.commit()需要在每个执行语句之后进行才能正确存储结果?这个想法是尽可能多地消除冗余语句,因为这个过程是长期运行的。
代码结构:
with connection.cursor() as cursor:
…
cursor.execute(SQLtype1, rowToInsert)
cursor.execute(SQLtype2, otherToInsert)
connection.commit() # does this account for both execute statements, or just the last?
Run Code Online (Sandbox Code Playgroud)
我已经审查了以下内容:
PyMySQL 执行/提交示例,但只有一个示例只有一个执行语句。
Python MySQLdb 示例,但有一个示例显示每个执行语句后的提交
Python SQLite 示例,显示提交前的多个执行语句,但不确定 SQLite 是否以不同方式处理
注意:由于 SQL 查询不同,executemany似乎不是一个选项。
我刚刚创建了一个由 Flask 提供支持的电子应用程序。
当我在 powershell 中运行该应用程序时,它运行得很好,但是当我使用 electro-packager 构建该应用程序时,它成功了,但该应用程序无法运行。
看来 python 代码不会包含在应用程序中。如何构建应用程序并集成我在应用程序中使用的所有 python 代码和模块?
我正在使用任何 python 模块,例如 pandas