小编Ore*_*ail的帖子

"退格"转义字符'\ b':意外行为?

所以我终于通过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)

有谁能解释一下?

c language-agnostic printf special-characters backspace

91
推荐指数
4
解决办法
13万
查看次数

下载并解压缩内存中的gzip压缩文件?

我想使用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)

python gzip file urllib2 stringio

38
推荐指数
3
解决办法
4万
查看次数

程序"是"用于任何重要的事情吗?

当我第一次看到yes*nix中的程序时,我想不到它的重要用途.

这个程序是如何使用的?

unix linux

15
推荐指数
2
解决办法
5247
查看次数

Cygwin的标准#ifdef

如果我想知道我是否正在编译Cygwin,那么要检查的标准宏是什么?

我已经看过#ifdef _WIN32,但这似乎没有面向未来或Cygwin具体.

c macros cygwin c-preprocessor

10
推荐指数
1
解决办法
4971
查看次数

在执行时读取子进程的stderr

我想读一下子进程在执行时写入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 buffer subprocess stderr

6
推荐指数
1
解决办法
8059
查看次数

在这种情况下,为什么Python比C++更快?

下面给出了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)

c++ python io performance

6
推荐指数
1
解决办法
836
查看次数

速度最好的网络图形爬虫?

在过去的一个月里,我一直在使用Scrapy进行我已经开始的网络爬行项目.

此项目涉及在主页中可以访问的单个域名中提取所有网页的完整文档内容.使用Scrapy写这个很容易,但它运行得太慢了.在2-3天内,我只能下载100,000页.

我已经意识到我最初认为Scrapy不适合这种类型的爬行的想法是揭示自己.

我开始把注意力集中在Nutch和Methabot上,希望能有更好的表现.我在爬网期间需要存储的唯一数据是网页的完整内容,最好是页面上的所有链接(但即使这样也可以在后处理中完成).

我正在寻找一种快速并且使用许多并行请求的爬虫.

web-crawler nutch scrapy

5
推荐指数
1
解决办法
2522
查看次数

如何生成X轴无序的Matplotlib图?

我想用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值.

我怎样才能做到这一点?

python matplotlib

4
推荐指数
1
解决办法
5410
查看次数

超类__init__没有认出它的kwargs

我正在尝试使用提供的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'

可能是对如何使用它的疏忽.

python multithreading subclass kwargs

4
推荐指数
1
解决办法
1271
查看次数

macOS Mojave上的x86_64程序集退出系统调用参数?

我有以下文件:

; 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)

macos assembly x86-64 system-calls mach

4
推荐指数
1
解决办法
78
查看次数