在尝试grep包含日志中的多行行的消息时,我遇到了以下错误...任何人都可以提供有关如何克服此错误的输入吗?
码:-
print gerrit_commitmsg
gerritlog = Popen('git','log','--grep','gerrit_commitmsg', stdout=PIPE, stderr=PIPE)
print gerritlog
Run Code Online (Sandbox Code Playgroud)
错误:-
Commit message:-
Build system changes
Build system changes to include packages in the build
Change-Id: I697558f01ae367d2baacdf2c7fcf1a03753edacd
Traceback (most recent call last):
File "gerrits_in_workspace.py", line 87, in <module>
main()
File "gerrits_in_workspace.py", line 77, in main
grep_commitmsg(gerrit_commitmsg)
File "gerrits_in_workspace.py", line 48, in grep_commitmsg
gerritlog = Popen('git','log','--grep','gerrit_commitmsg', stdout=PIPE, stderr=PIPE)
File "/usr/lib/python2.7/subprocess.py", line 629, in __init__
raise TypeError("bufsize must be an integer")
Run Code Online (Sandbox Code Playgroud) 做Python的str.__lt__或sorted顺序字符根据其Unicode索引或某些区域设置相关的排序规则?
我试图从巴西Portogese文本解码:
'Demais Subfun\xc3\xa7\xc3\xb5es 12'
它应该是
'DemaisSubfunções12'
>> a.decode('unicode_escape')
>> a.encode('unicode_escape')
>> a.decode('ascii')
>> a.encode('ascii')
Run Code Online (Sandbox Code Playgroud)
所有给予:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 13:
ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
另一方面,这给出了:
>> print a.encode('utf-8')
Demais Subfun?â?º?â?Áes 12
>> print a
Demais Subfunções 12
Run Code Online (Sandbox Code Playgroud) 我正在为我的一个项目编写一个shell,它通过设计解析看起来像这样的命令:
COMMAND_NAME ARG1 ="长值"ARG2 = 123 ARG3=me@me.com
我的问题是Python的命令行解析库(getopt和optparse)迫使我在参数前面使用' - '或' - '.此行为与我的要求不符.
任何想法如何解决?任何现有的库?
Ruby中以下代码的Python等价物是什么?
def loop
cont=nil
for i in 1..4
puts i
callcc {|continuation| cont=continuation} if i==2
end
return cont
end
> c=loop
1
2
3
4
> c.call
3
4
Run Code Online (Sandbox Code Playgroud)
如何确定任何列表元素是否是dict的关键?直截了当的方式是,
for i in myList:
if i in myDict:
return True
return False
Run Code Online (Sandbox Code Playgroud)
但是有更快/更简洁的方式吗?
如果print s被替换,print >>sys.stderr, s则效果消失.
import random, sys, time
import threading
lock = threading.Lock()
def echo(s):
time.sleep(1e-3*random.random()) # instead of threading.Timer()
with lock:
print s
for c in 'abc':
threading.Thread(target=echo, args=(c,)).start()
Run Code Online (Sandbox Code Playgroud)
# Run until empty line is found:
$ while ! python example.py 2>&1|tee out|grep '^$';do echo -n .;done;cat out
Run Code Online (Sandbox Code Playgroud)
....................
b
c
a
Run Code Online (Sandbox Code Playgroud)
输出不应包含空行,但确实如此.我知道这print不是线程安全的,但我认为锁应该有所帮助.
我的机器:
$ python -mplatform
Linux-2.6.38-11-generic-x86_64-with-Ubuntu-11.04-natty
Run Code Online (Sandbox Code Playgroud)
额外的线条印在py26,py27,pypy上.
py24,py25,py31,py32表现得如预期的那样(没有空行).
sys.stdout.flush()之后print没有解决问题:
with lock:
print(s) …Run Code Online (Sandbox Code Playgroud)我正在尝试使用Cython优化我的代码.它正在做一个功率谱,而不是使用FFT,因为这是我们在课堂上被告知要做的事情.我曾尝试在Cython中编写代码,但没有看到任何区别.这是我的代码
#! /usr/bin/env python
# -*- coding: utf8 -*-
from __future__ import division
cimport numpy as np
import numpy as np
cimport cython
@cython.boundscheck(False)
def power_spectrum(time, data, double f_min, double f_max, double df,w=1 ):
cdef double com,f
cdef double s,c,sc,cc,ss
cdef np.ndarray[double, ndim=1] power
cdef np.ndarray[double, ndim=1] freq
alfa, beta = [],[]
m = np.mean(data)
data -= m
freq = np.arange( f_min,f_max,df )
for f in freq:
sft = np.sin(2*np.pi*f*time)
cft = np.cos(2*np.pi*f*time)
s = np.sum( w*data*sft )
c = …Run Code Online (Sandbox Code Playgroud) 我有这个 JavaScript 代码:
(new Date()).getTime()
Run Code Online (Sandbox Code Playgroud)
和这个Python代码:
time.time()
Run Code Online (Sandbox Code Playgroud)
第一个返回类似1410072754803的东西,但 python 返回类似1410077369.27的东西,我需要 python 返回与 javascript 相同的格式。我怎样才能得到它?
PS:如果我使用
time.time() * 1000
Run Code Online (Sandbox Code Playgroud)
它返回:
1.41007799536e+12
Run Code Online (Sandbox Code Playgroud) 在Fabric 1中,它看起来像这样:
with settings(warn_only=True):
run_commands_that_may_fail()
Run Code Online (Sandbox Code Playgroud)
目前还不清楚如何在Fabric 2中通过上下文管理器pyinvoke实现它。升级文档建议替换warn_only为run.warn. 我想出了:
old_warn = c.config.run.warn
c.config.run.warn = True
try:
run_commands_that_may_fail(c)
finally:
c.config.run.warn = old_warn
Run Code Online (Sandbox Code Playgroud)
也许,有一种更好的方法,类似于 Fabric 的 1。
python ×10
unicode ×2
arguments ×1
buffering ×1
command-line ×1
cython ×1
dictionary ×1
fabric ×1
io ×1
javascript ×1
list ×1
optimization ×1
parsing ×1
python-2.x ×1
python-3.x ×1
ruby ×1
shell ×1
spectrum ×1
string ×1
subprocess ×1
time ×1