这段代码几乎可以满足我的需要..
for line in all_lines:
s = line.split('>')
Run Code Online (Sandbox Code Playgroud)
除了它删除所有'>'分隔符.
所以,
<html><head>
Run Code Online (Sandbox Code Playgroud)
变成
['<html','<head']
Run Code Online (Sandbox Code Playgroud)
有没有办法使用split()方法但保留分隔符,而不是删除它?
有了这些结果..
['<html>','<head>']
Run Code Online (Sandbox Code Playgroud) http://learnpythonthehardway.org/book/ex6.html
Zed似乎在这里使用%r和%s互换,两者之间有什么区别吗?为什么不一直使用%s?
此外,我不知道在文档中搜索什么以查找更多信息.什么是%r和%s究竟叫什么名字?格式化字符串?
我正在尝试编写一个脚本来测试网页是否存在,如果不下载整个页面就会检查它会很好.
这是我的跳跃点,我已经看到多个示例以相同的方式使用httplib,但是,我检查的每个站点都返回false.
import httplib
from httplib import HTTP
from urlparse import urlparse
def checkUrl(url):
p = urlparse(url)
h = HTTP(p[1])
h.putrequest('HEAD', p[2])
h.endheaders()
return h.getreply()[0] == httplib.OK
if __name__=="__main__":
print checkUrl("http://www.stackoverflow.com") # True
print checkUrl("http://stackoverflow.com/notarealpage.html") # False
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
编辑
有人建议这个,但他们的帖子被删除.. urllib2是否避免下载整个页面?
import urllib2
try:
urllib2.urlopen(some_url)
return True
except urllib2.URLError:
return False
Run Code Online (Sandbox Code Playgroud) 我正在使用新的virtualenv,并尝试在其上安装django.
当我输入时,which django-admin.py我回来了/usr/local/bin/django-admin.py,这是不正确的,它应该安装在.virtualenvs文件夹中.
如果我这样做,pip install django我会回来:
Requirement already satisfied (use --upgrade to upgrade): django in /Library/Python/2.6/site-packages
Run Code Online (Sandbox Code Playgroud)
如果我这样做,pip uninstall django我会回来:
Not uninstalling Django at /Library/Python/2.6/site-packages, outside environment /Users/username/.virtualenvs/envname/bin/..
Run Code Online (Sandbox Code Playgroud)
如何在virtualenv中安装和使用django?不知道如何解决这个问题.
python的新手,并试图学习文件i/o的绳索.
我正在使用以下格式从大型(200万行)文件中提取行:
56fr4
4543d
4343d
5irh3
Run Code Online (Sandbox Code Playgroud)
这是我用来返回代码的函数:
def getCode(i):
with open("test.txt") as f:
for index, line in enumerate(f):
if index == i:
code = # what does it equal?
break
return code
Run Code Online (Sandbox Code Playgroud)
一旦索引到达正确的位置(i),我使用什么语法来设置代码变量?
我正在运行一个脚本,正在测试一系列网址的可用性。
这是功能之一。
def checkUrl(url): # Only downloads headers, returns status code.
p = urlparse(url)
conn = httplib.HTTPConnection(p.netloc)
conn.request('HEAD', p.path)
resp = conn.getresponse()
return resp.status
Run Code Online (Sandbox Code Playgroud)
有时,VPS 会失去连接,发生这种情况时整个脚本会崩溃。
File "/usr/lib/python2.6/httplib.py", line 914, in request
self._send_request(method, url, body, headers)
File "/usr/lib/python2.6/httplib.py", line 951, in _send_request
self.endheaders()
File "/usr/lib/python2.6/httplib.py", line 908, in endheaders
self._send_output()
File "/usr/lib/python2.6/httplib.py", line 780, in _send_output
self.send(msg)
File "/usr/lib/python2.6/httplib.py", line 739, in send
self.connect()
File "/usr/lib/python2.6/httplib.py", line 720, in connect
self.timeout)
File "/usr/lib/python2.6/socket.py", line 561, in create_connection
raise error, msg …Run Code Online (Sandbox Code Playgroud) 我今晚第一次在Macbook上安装了Ada和AWS.
安装似乎一切都很成功,但我感觉AWS没有安装到正确的目录.
GNAT位于/ usr/local/gnat中.
AWS makefile.conf说,
AWS will be installed under $(prefix). Update this variable to point to
the install directory. By default, it is set to the current GNAT root
directory to have AWS project files automatically available.
Run Code Online (Sandbox Code Playgroud)
所以我没有改变目标.
但是当我尝试从系统中除AWS demos文件夹之外的任何地方编译hello_world时,我收到此错误:
~/projects/ada:gnatmake hello.adb
gcc -c hello.adb
hello.adb:1:06: file "aws.ads" not found
hello.adb:2:06: file "aws.ads" not found
hello.adb:3:06: file "aws.ads" not found
hello.adb:4:06: file "aws.ads" not found
gnatmake: "hello.adb" compilation error
Run Code Online (Sandbox Code Playgroud)
如何确保AWS已正确安装,或者如果不正确则移动它?
python的新手,并试图学习文件i/o的绳索.
使用以下格式从大型(200万行)文件中拉取行:
56fr4
4543d
4343d
hirh3
Run Code Online (Sandbox Code Playgroud)
我一直在读readline()是最好的,因为它不会将整个文件拉入内存.但是当我尝试阅读它上面的文档时,它似乎只是Unix?我在Mac上.
我可以在Mac上使用readline而无需将整个文件加载到内存中吗?简单地在文件中读取数字3的语法是什么?文档中的示例有点过头了.
编辑
这是返回代码的函数:
def getCode(i):
with open("test.txt") as file:
for index, line in enumerate(f):
if index == i:
code = # what does it equal?
break
return code
Run Code Online (Sandbox Code Playgroud) def download(url):
print url
user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11'
headers = {'User-Agent' : user_agent }
request = urllib2.Request(url, headers)
response = urllib2.urlopen(request)
return response
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?我正在使用文档中的确切示例:
rare = (["word1","word4","word5"])
freq = (["word1","word2","word3"])
unique = rare.intersection(freq)
print unique
Run Code Online (Sandbox Code Playgroud)
错误:AttributeError:'list'对象没有属性'intersection'
我没有正确创建套装吗?它们看起来像文档中的示例 - 但我似乎无法对它们使用常规的set方法.
如果这些是列表,创建集合的正确语法是什么?
这是我目前的代码:
dfile = open('dictionary.txt', 'r')
sfile = open('substrings.txt', 'r')
dictionary_words = []
substrings = []
for line in dfile:
dictionary_words.append(line.rstrip('\n'))
for line in sfile:
substrings.append(line.rstrip('\n'))
Run Code Online (Sandbox Code Playgroud)
它有效,但看起来很罗嗦.
这是写出来的合适方式,还是我错过了一个更简单的过程?
python ×9
file-io ×3
installation ×2
list ×2
ada ×1
delimiter ×1
django ×1
enumerate ×1
header ×1
httplib ×1
intersection ×1
macos ×1
networking ×1
pip ×1
set ×1
sockets ×1
split ×1
strip ×1
urllib2 ×1
urlparse ×1
virtualenv ×1
whitespace ×1