看起来像Numpy的唯一64位Windows安装程序是Numpy版本1.3.0,它只适用于Python 2.6
http://sourceforge.net/projects/numpy/files/NumPy/
令我感到奇怪的是,我必须回滚到Python 2.6才能在Windows上使用Numpy,这让我觉得我错过了一些东西.
我呢?
在Python中,您可以将StringIO用于字符数据的类文件缓冲区.内存映射文件基本上对二进制数据做类似的事情,但它需要一个用作基础的文件.Python是否有一个用于二进制数据的文件对象,并且只是内存,相当于Java的ByteArrayOutputStream?
我的用例是我想在内存中创建一个ZIP文件,ZipFile需要一个类似文件的对象.
我想尝试python BytesIO类.
作为一个实验,我尝试写入内存中的zip文件,然后从该zip文件中读取字节.因此gzip
,我传入一个BytesIO
对象,而不是传入一个文件对象.这是整个脚本:
from io import BytesIO
import gzip
# write bytes to zip file in memory
myio = BytesIO()
g = gzip.GzipFile(fileobj=myio, mode='wb')
g.write(b"does it work")
g.close()
# read bytes from zip file in memory
g = gzip.GzipFile(fileobj=myio, mode='rb')
result = g.read()
g.close()
print(result)
Run Code Online (Sandbox Code Playgroud)
但它正在返回一个空bytes
对象result
.这在Python 2.7和3.4中都会发生.我错过了什么?
如何使用Python subprocess
模块执行以下shell命令?
echo "input data" | awk -f script.awk | sort > outfile.txt
Run Code Online (Sandbox Code Playgroud)
输入数据将来自一个字符串,所以我实际上并不需要echo
.我已经走到这一步了,任何人都可以解释我是如何通过它来解决的sort
吗?
p_awk = subprocess.Popen(["awk","-f","script.awk"],
stdin=subprocess.PIPE,
stdout=file("outfile.txt", "w"))
p_awk.communicate( "input data" )
Run Code Online (Sandbox Code Playgroud)
更新:请注意,虽然下面接受的答案实际上没有回答问题,但我相信S.Lott是对的,最好避免首先解决这个问题!
我正在使用生成器在列表中执行搜索,就像这个简单的例子:
>>> a = [1,2,3,4]
>>> (i for i, v in enumerate(a) if v == 4).next()
3
Run Code Online (Sandbox Code Playgroud)
(只是为了对示例进行框架化,我使用的列表与上面的列表相比要长得多,而且条目有点复杂int
.我这样做,所以每次我都不会遍历整个列表搜索他们)
现在如果我改为那样i == 666
,它会返回一个,StopIteration
因为它找不到任何666
条目a
.
我怎样才能让它返回None
?我当然可以将它包装在一个try ... except
子句中,但有更多的pythonic方法吗?
我正在尝试在Windows 64位机器上为Python 2.7构建lxml.我找不到Python 2.7版本的lxml egg.所以我从源头编译它.我正在按照本网站上的说明操作
在静态链接部分下.我收到了错误
C:\Documents and Settings\Administrator\Desktop\lxmlpackage\lxml-2.2.6\lxml-2.2.
6>python setup.py bdist_wininst --static
Building lxml version 2.2.6.
NOTE: Trying to build without Cython, pre-generated 'src/lxml/lxml.etree.c' need
s to be available.
ERROR: 'xslt-config' is not recognized as an internal or external command,
operable program or batch file.
** make sure the development packages of libxml2 and libxslt are installed **
Using build configuration of libxslt
Building against libxml2/libxslt in one of the following directories:
..\libxml2-2.7.6--win32--w2k--x64\lib
..\libxslt-1.1.26--win32--w2k--x64--0002\lib
..\zlib-1.2.4--win32--w2k--x64
..\iconv-1.9.1--win32--w2k--x64-0001\lib
running bdist_wininst …
Run Code Online (Sandbox Code Playgroud) 我正在编写一个脚本,需要检查特定提交是否是合并/恢复提交,我想知道是否有一个git技巧.
到目前为止我提出的(我绝对不想依赖于提交消息)是检查HASH^2
并查看我是否没有收到错误,是否有更好的方法?
为什么在示例函数中终止:
def func(iterable):
while True:
val = next(iterable)
yield val
Run Code Online (Sandbox Code Playgroud)
但如果我脱掉yield语句函数会引发StopIteration异常吗?
编辑:很抱歉误导你们.我知道什么是发电机以及如何使用它们.当然,当我说功能终止时,我并不意味着急切的功能评估.我只是暗示当我使用函数生成生成器时:
gen = func(iterable)
Run Code Online (Sandbox Code Playgroud)
在func的情况下它工作并返回相同的生成器,但在func2的情况下:
def func2(iterable):
while True:
val = next(iterable)
Run Code Online (Sandbox Code Playgroud)
它引发StopIteration而不是None返回或无限循环.
让我更具体一点.itertools中有一个函数tee,相当于:
def tee(iterable, n=2):
it = iter(iterable)
deques = [collections.deque() for i in range(n)]
def gen(mydeque):
while True:
if not mydeque: # when the local deque is empty
newval = next(it) # fetch a new value and
for d in deques: # load it to all the …
Run Code Online (Sandbox Code Playgroud) 说我想要替换的所有比赛Mr.
,并Mr
用Mister
.
我使用以下正则表达式:\bMr(\.)?\b
匹配Mr.
或只是匹配Mr
.然后,我使用该re.sub()
方法进行替换.
更令人不解的我的是,它正在取代Mr.
用Mister.
.为什么这会保持点.
到底?看起来它不符合Mr\.
案例但只是Mr
.
import re
s="a rMr. Nobody Mr. Nobody is Mr Nobody and Mra Nobody."
re.sub(r"\bMr(\.)?\b","Mister", s)
Run Code Online (Sandbox Code Playgroud)
返回:
'a rMr. Nobody Mister. Nobody is Mister Nobody and Mra Nobody.'
Run Code Online (Sandbox Code Playgroud)
我也试过以下,但也没有运气:
re.sub(r"\b(Mr\.|Mr)\b","Mister", s)
Run Code Online (Sandbox Code Playgroud)
我想要的输出是:
'a rMr. Nobody Mister Nobody is Mister Nobody and Mra Nobody.'
^ ^
no dot this should be kept as it …
Run Code Online (Sandbox Code Playgroud) 当我输入简单代码时:
import datetime
datetime.utcnow()
Run Code Online (Sandbox Code Playgroud)
,我收到错误信息:
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
datetime.utcnow()
AttributeError: 'module' object has no attribute 'utcnow'
Run Code Online (Sandbox Code Playgroud)
但是python的文档utcnow
就在这里:https://docs.python.org/library/datetime.html#datetime.datetime.utcnow.为什么utcnow
我的电脑无法正常工作?谢谢!