Compare strings in python like the sql "like" (with "%" and "

I have a list in python with some strings, and I need to know witch item in the list is like "A1_8301". This "_" means that can be any char. Is there a quick way to do that? If I was using SQL, i just type something like "where x like "A1_8301" Thank you! In Python you'd use a regular expression: import re pattern = re.compile(r'^A1.8301$') matches

比较python中的字符串,如sql“like”(带有“%”和“

我在Python中有一些列表,并且我需要知道列表中的女巫项目是“A1_8301”。 这个“_”意味着可以是任何字符。 有没有快速的方法来做到这一点? 如果我使用的是SQL,我只需键入“where x like”A1_8301“ 谢谢! 在Python中你可以使用正则表达式: import re pattern = re.compile(r'^A1.8301$') matches = [x for x in yourlist if pattern.match(x)] 这会生成符合您要求的元素列表。 需要使用^和$锚来防止子串匹配; 例如

How to open a new default browser window in Python when the default is Chrome

I have been looking for a way to open a new default browser window from inside Python code. According to the documentation webbrowser.open_new(url) Should do that. Unfortunately in case Chrome is the default browser it only opens a new tab. Is there any way to open the default browser (without knowing what that browser is)? 给这个旋转: import subprocess command = "cmd /c start chrome http:/

如果默认为Chrome,如何在Python中打开一个新的默认浏览器窗口

我一直在寻找一种方法来在Python代码中打开一个新的默认浏览器窗口。 根据文档webbrowser.open_new(url)应该这样做。 不幸的是,如果Chrome浏览器是默认浏览器,则只会打开一个新标签页。 有什么方法可以打开默认浏览器(不知道该浏览器是什么)? 给这个旋转: import subprocess command = "cmd /c start chrome http://www.ebay.com --new-window" subprocess.Popen(command, shell=True) 我有一种感觉,这不是Python

Embed a web browser in a Python program

How can I embed a web browser in a Python program? It needs to run on Linux (GTK, Qt are fine), or cross-platform. I have looked at embedding pywebgtk and Qt's WebKit widget. But these seem to have little more than a rendering engine. In particular, I'd like support for back/forward and tabbed browsing. Is something like this pre-packaged, or do I have to implement it myself? wxWe

在Python程序中嵌入一个Web浏览器

我如何在Python程序中嵌入网页浏览器? 它需要在Linux上运行(GTK,Qt都可以)或者跨平台的。 我已经看过嵌入pywebgtk和Qt的WebKit小部件。 但是这些似乎只有一个渲染引擎。 特别是,我希望支持后退/前进和标签式浏览。 是这样的预先打包,还是我必须自己实现? wxWebConnect似乎大概是我想的,但它没有Python绑定。 http://pypi.python.org/pypi/selenium/2.7.0 您可以安装selenium软件包并运行与您的python代码连

How can I get all days between two days?

I need all the weekdays between two days. Example: Wednesday - Friday = Wednesday, Thursday, Friday 3 - 5 = 3, 4, 5 Saturday - Tuesday = Saturday, Sunday, Monday, Tuesday 6 - 2 = 6, 7, 1, 2 I'm pretty sure there is a clever algorithm out there to solve this. The only algorithms I can think of use either a loop or an if statement. There has to be an ele

我怎样才能在两天之间得到所有的日子?

我需要两天之间的所有工作日。 例: Wednesday - Friday = Wednesday, Thursday, Friday 3 - 5 = 3, 4, 5 Saturday - Tuesday = Saturday, Sunday, Monday, Tuesday 6 - 2 = 6, 7, 1, 2 我很确定有一个聪明的算法来解决这个问题。 我能想到的唯一算法使用循环或if语句。 必须有一个优雅的方式来解决这个问题。 我在平日使用数字1-7,但0-6也很好。 我能想到的最好的: def between(

Python Unittest Import Failure

Im trying to set up my Python3 project to work with test discovery, as described here and covered in this question My project structure is as follows: youtube/ youtube/ __init__.py videos.py categories.py test/ test_videos.py test_categories.py In my tests I've imported the modules to be tested with their fully qualified names, eg for test_v

Python单元测试导入失败

我试图设置我的Python3项目来处理测试发现,如此处所述并在此问题中所述 我的项目结构如下: youtube/ youtube/ __init__.py videos.py categories.py test/ test_videos.py test_categories.py 在我的测试中,我已经导入了要使用完全限定名称进行测试的模块,例如test_videos.py我的第一个导入是import youtube.videos as videos 。 我遇到的问题是videos模块导入catego

Running python script inside ipython

Is it possible to run a python script (not module) from inside ipython without indicating its path? I tried to set PYTHONPATH but it seems to work only for modules. I would like to execute %run my_script.py without being in the directory containing the file. 从“my_script.py”的目录中,您可以简单地执行以下操作: %run ./my_script.py How to run a script in Ipython import os filepath='C:\Users\

在ipython内运行python脚本

是否有可能在ipython中运行python脚本(而不是模块)而不指示其路径? 我试图设置PYTHONPATH,但它似乎只适用于模块。 我想执行 %run my_script.py 而不在包含该文件的目录中。 从“my_script.py”的目录中,您可以简单地执行以下操作: %run ./my_script.py 如何在Ipython中运行脚本 import os filepath='C:\Users\User\FolderWithPythonScript' os.chdir(filepath) %run pyFileInThatFilePath.py 这应该做到这一点 在

Python package structure, setup.py for running unit tests

I'm not sure I'm organizing my package structure correctly or am using the right options in setup.py because I'm getting errors when I try to run unit tests. I have a structure like this: /project /bin /src /pkgname __init__.py module1.py module2.py /tests __init__.py test1.py test2.py My setup.py look

Python程序包结构,运行单元测试的setup.py

我不确定我是否正确地组织了我的包结构,或者在setup.py中使用了正确的选项,因为我在尝试运行单元测试时遇到错误。 我有这样的结构: /project /bin /src /pkgname __init__.py module1.py module2.py /tests __init__.py test1.py test2.py 我的setup.py如下所示: #!/usr/bin/env python

How do I run all Python unit tests in a directory?

I have a directory that contains my Python unit tests. Each unit test module is of the form test_*.py . I am attempting to make a file called all_test.py that will, you guessed it, run all files in the aforementioned test form and return the result. I have tried two methods so far; both have failed. I will show the two methods, and I hope someone out there knows how to actually do this corre

如何在目录中运行所有Python单元测试?

我有一个包含我的Python单元测试的目录。 每个单元测试模块的形式为test _ *。py 。 我试图创建一个名为all_test.py的文件,你会猜到它会运行上述测试表单中的所有文件并返回结果。 到目前为止,我尝试了两种方法; 都失败了。 我将展示这两种方法,并希望有人知道如何正确地做到这一点。 对于我第一次勇敢的尝试,我认为“如果我只是将所有测试模块导入到文件中,然后调用这个unittest.main()装饰物,它会起作用,对吧?”

Django Import Error: No module named apps

I just checked out a project with git. The project structure is project apps myapp settings __init__.py __init__.py manage.py There are other directories and files, but I think those are the important ones. When I run the server I get Traceback (most recent call last): File "C:/Dev/project/apps/manage.py", line 10, in <module> execute_from_comma

Django导入错误:没有名为apps的模块

我刚刚用git检出了一个项目。 项目结构是 project apps myapp settings __init__.py __init__.py manage.py 还有其他的目录和文件,但我认为这些是重要的。 当我运行我得到的服务器 Traceback (most recent call last): File "C:/Dev/project/apps/manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "C:Python27libsite-packagesdjangocoremana

Import Python Script Into Another?

I'm going through Zed Shaw's Learn Python The Hard Way and I'm on lesson 26. In this lesson we have to fix some code, and the code calls functions from another script. He says that we don't have to import them to pass the test, but I'm curious as to how we would do so. Link to the lesson | Link to the code to correct And here are the particular lines of code that call on

导入Python脚本到另一个?

我正在经历Zed Shaw的“学习Python困难的方法”,我正在上课26.在本课中,我们必须修复一些代码,并且代码会从另一个脚本调用函数。 他说我们不需要导入它们来通过测试,但我很好奇我们会怎么做。 链接到课程| 链接到代码来纠正 以下是调用以前脚本的特定代码行: words = ex25.break_words(sentence) sorted_words = ex25.sort_words(words) print_first_word(words) print_last_word(words) print_first_word(sorted_word