所以我终于通过K&R阅读,并且我在前几页中学到了一些东西,即有一个退格转义字符\b.
所以我去测试它,并且有一些非常奇怪的行为:
#include <stdio.h>
main ()
{
printf("hello worl\b\bd\n");
}
Run Code Online (Sandbox Code Playgroud)
输出是
hello wodl
Run Code Online (Sandbox Code Playgroud)
有谁能解释一下?
我想使用urllib下载文件并在保存之前将文件解压缩到内存中.
这就是我现在所拥有的:
response = urllib2.urlopen(baseURL + filename)
compressedFile = StringIO.StringIO()
compressedFile.write(response.read())
decompressedFile = gzip.GzipFile(fileobj=compressedFile, mode='rb')
outfile = open(outFilePath, 'w')
outfile.write(decompressedFile.read())
Run Code Online (Sandbox Code Playgroud)
这最终会写出空文件.我怎样才能实现我追求的目标?
更新答案:
#! /usr/bin/env python2
import urllib2
import StringIO
import gzip
baseURL = "https://www.kernel.org/pub/linux/docs/man-pages/"
# check filename: it may change over time, due to new updates
filename = "man-pages-5.00.tar.gz"
outFilePath = filename[:-3]
response = urllib2.urlopen(baseURL + filename)
compressedFile = StringIO.StringIO(response.read())
decompressedFile = gzip.GzipFile(fileobj=compressedFile)
with open(outFilePath, 'w') as outfile:
outfile.write(decompressedFile.read())
Run Code Online (Sandbox Code Playgroud) 如果我想知道我是否正在编译Cygwin,那么要检查的标准宏是什么?
我已经看过#ifdef _WIN32,但这似乎没有面向未来或Cygwin具体.
我想读一下子进程在执行时写入stderr的内容.
但是,当我使用我编写的这个脚本时,stderr似乎没有任何东西供我阅读,直到子进程退出.
#!/usr/bin/env python2
import sys
from subprocess import Popen, PIPE, STDOUT
if len(sys.argv) < 2:
print "Please provide a command"
sys.exit(1)
sub = Popen(sys.argv[1:], stdout=PIPE, stderr=STDOUT)
for i, line in enumerate(sub.stdout):
sys.stdout.write("%d: %s" % (i, line))
Run Code Online (Sandbox Code Playgroud)
编辑:
好的,我现在离得更近了.如果我指定要读取的字节数,则克服缓冲.
#!/usr/bin/env python2
import sys
from subprocess import Popen, PIPE, STDOUT
if len(sys.argv) < 2:
print "Please provide a command"
sys.exit(1)
sub = Popen(sys.argv[1:], stdout=PIPE, stderr=STDOUT)
i = 0
while sub.poll() is None:
line = sub.stdout.read(64)
line.strip("\b")
sys.stdout.write("%d: %s\n" % (i, line)) …Run Code Online (Sandbox Code Playgroud) 下面给出了Python和C++中的程序,它执行以下任务:从stdin读取空格分隔的单词,打印按字符串长度排序的唯一单词以及每个唯一单词到stdout的计数.输出行的格式为:length,count,word.
例如,使用此输入文件(488kB的同义词库) http://pastebin.com/raw.php?i=NeUBQ22T
带格式的输出是这样的:
1 57 "
1 1 n
1 1 )
1 3 *
1 18 ,
1 7 -
1 1 R
1 13 .
1 2 1
1 1 S
1 5 2
1 1 3
1 2 4
1 2 &
1 91 %
1 1 5
1 1 6
1 1 7
1 1 8
1 2 9
1 16 ;
1 2 =
1 5 A
1 1 C
1 5 e
1 …Run Code Online (Sandbox Code Playgroud) 在过去的一个月里,我一直在使用Scrapy进行我已经开始的网络爬行项目.
此项目涉及在主页中可以访问的单个域名中提取所有网页的完整文档内容.使用Scrapy写这个很容易,但它运行得太慢了.在2-3天内,我只能下载100,000页.
我已经意识到我最初认为Scrapy不适合这种类型的爬行的想法是揭示自己.
我开始把注意力集中在Nutch和Methabot上,希望能有更好的表现.我在爬网期间需要存储的唯一数据是网页的完整内容,最好是页面上的所有链接(但即使这样也可以在后处理中完成).
我正在寻找一种快速并且使用许多并行请求的爬虫.
我想用x轴绘制一些y数据,这些数据不是按数字顺序出现的:
例如:
y = [100, 99, 93, 88, 85, 43]
x = [0, 5, 4, 3, 2, 1]
plot(x, y)
Run Code Online (Sandbox Code Playgroud)
我希望x轴按顺序显示为0,5,4,3,2,1,均匀间隔,上面有适当的y值.
我怎样才能做到这一点?
我正在尝试使用提供的StoppableThread课程作为另一个问题的答案:
import threading
# Technique for creating a thread that can be stopped safely
# Posted by Bluebird75 on StackOverflow
class StoppableThread(threading.Thread):
"""Thread class with a stop() method. The thread itself has to check
regularly for the stopped() condition."""
def __init__(self):
super(StoppableThread, self).__init__()
self._stop = threading.Event()
def stop(self):
self._stop.set()
def stopped(self):
return self._stop.isSet()
Run Code Online (Sandbox Code Playgroud)
但是,如果我运行如下:
st = StoppableThread(target=func)
Run Code Online (Sandbox Code Playgroud)
我明白了:
TypeError:
__init__()得到一个意外的关键字参数'target'
可能是对如何使用它的疏忽.
我有以下文件:
; hello.s
.section __TEXT,__text
.globl _main
_main:
movl $0x2000001, %eax
movl $42, %ebx
syscall
Run Code Online (Sandbox Code Playgroud)
我尝试按如下方式运行它:
# run.sh
as -mmacosx-version-min=10.9 hello.s -o hello.o
ld -macosx_version_min 10.9 -lSystem hello.o -e _main -o hello
./hello
echo $?
Run Code Online (Sandbox Code Playgroud)
输出是:
$ ./run.sh
1
Run Code Online (Sandbox Code Playgroud)
我希望它是
$ ./run.sh
42
Run Code Online (Sandbox Code Playgroud)
这有什么不对?
编辑:
根据zneak的回答,我们需要在系统调用中使用%edi寄存器,因此工作程序是:
; hello.s
.section __TEXT,__text
.globl _main
_main:
movl $0x2000001, %eax
movl $42, %edi
syscall
Run Code Online (Sandbox Code Playgroud) python ×5
c ×2
assembly ×1
backspace ×1
buffer ×1
c++ ×1
cygwin ×1
file ×1
gzip ×1
io ×1
kwargs ×1
linux ×1
mach ×1
macos ×1
macros ×1
matplotlib ×1
nutch ×1
performance ×1
printf ×1
scrapy ×1
stderr ×1
stringio ×1
subclass ×1
subprocess ×1
system-calls ×1
unix ×1
urllib2 ×1
web-crawler ×1
x86-64 ×1