我期待用perl正则表达式/ e选项替换哈希值.以下代码对我不起作用.
#!/usr/bin/perl
%hash=( 1 => "aa", 2 => "bb", 3 => "cc" );
$_="name,3";
s/^(name,(\d+).*)/$2,$hash{$1}/e;
print "$_\n";
exit 0;
Run Code Online (Sandbox Code Playgroud)
我期待这样的输出:
name,3,cc
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
谢谢
我什么时候可以通过*
并且**
在Python函数的参数?即:
def fun_name(arg1, *arg2 , ** arg3):
Run Code Online (Sandbox Code Playgroud) 我一直在尝试优化以下两个嵌套循环:
def startbars(query_name, commodity_name):
global h_list
nc, s, h_list = [], {}, {}
query = """ SELECT wbcode, Year, """+query_name+"""
FROM innovotable WHERE commodity='"""+commodity_name+"""' and
"""+query_name+""" != 'NULL' """
rows = cursor.execute(query)
for row in rows:
n = float(row[2])
s[str(row[0])+str(row[1])] = n
nc.append(n)
for iso in result:
try:
for an_year in xrange(1961, 2031, 1):
skey = iso+str(an_year)
h_list[skey] = 8.0 / max(nc) * s[skey]
except:
pass
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?谢谢.
在模块中,我使用以下__init__
方法定义了一个类:
class RMLoader(object):
def __init__(self):
self.now=datetime.datetime.now()
Run Code Online (Sandbox Code Playgroud)
然后我在控制台中导入该模块:
>>> from video.remmedia.loader import RMLoader
>>> loader=RMLoader()
>>> loader.now
datetime.datetime(2010, 11, 4, 17, 40, 36, 523000)
Run Code Online (Sandbox Code Playgroud)
问题是为什么它不给我标准的日期时间对象?
但是当我打印它时,它就像标准的datetime对象一样:
>>> print loader.now
2010-11-04 17:40:36.523000
Run Code Online (Sandbox Code Playgroud)
但是我不能在我需要datetime对象的函数中使用它...
如何像标准日期时间一样使用该属性?
我有一个实体数组,需要在一个大的sting中替换,但只有第一次出现(这就是为什么我使用preg_replace
而不是str_replace
),例如:
$entities = array();
$entities[0] = 'string1';
$entities[1] = 'string2';
$entities[2] = 'string2';
$entities[3] = 'Error String ('; ## this is the one that errors because of the bracket
$entities[4] = 'string4';
$entities[5] = 'string5';
foreach ($entities as $entity) {
$new_article = preg_replace('/' . $entity . '/', '##' . $key, $new_article, 1);
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Warning (2): preg_replace() [function.preg-replace]: Compilation failed: missing ) at offset XX
Run Code Online (Sandbox Code Playgroud)
使括号转义的最佳方法是什么,并且还可以转义可能在正则表达式中使用的任何其他字符.
谢谢
我搜索了论坛,发现了类似的问题,但没有运气解决我的问题.
我的代码旨在使用递归交换每个单词的每两个字母并打印结果.对于具有偶数字母的单词,输出中包含单词"None",我不知道如何修复...
这是代码:
def encryptLine(line, count):
headline = line[count:]
if length(headline) > 0:
if count == length(line) - 1:
new = headline
return new
elif count <= length(line):
new = head(tail(headline)) + head(headline)
new = new + str(encryptLine(line, count+2))
return new
print(encryptLine('abcd', 0))
Run Code Online (Sandbox Code Playgroud)
'abcd'的输出是badcNone,除了单词None之外,这是正确的.'abcde'的输出是'badce',这是正确的......
在此先感谢您的帮助!
错误:
警告:preg_match_all()[function.preg-match-all]:第23行/Users/julian/Sites/abc.php中的未知修饰符'g'警告:preg_match_all()[function.preg-match-all]:未知第23行/Users/julian/Sites/abc.php中的修饰符'g'
这是我的代码:
<?php
class Crawler {
protected $markup = ”;
public function __construct($uri) {
$this->markup = $this->getMarkup($uri);
}
public function getMarkup($uri) {
return file_get_contents($uri);
}
public function get($type) {
$method = "_get_links";
if (method_exists($this, $method))
return call_user_method($method, $this);
}
}
protected function _get_images() {
if (!empty($this->markup)){
preg_match_all(htmlspecialchars("<img([^>]+)/>i"), $this->markup, $images);
return $images[1];
}
}
protected function _get_links() {
if (!empty($this->markup)){
preg_match_all(htmlspecialchars("<a([^>]+)>(.*?)</a>/i"), $this->markup, $links);
return $links;
}
}
}
$crawl = new Crawler("http://google.com/");
$images = $crawl->get(‘images’);
$links = $crawl->get(‘links’);
echo $links; …
Run Code Online (Sandbox Code Playgroud) 我有一个元组列表,如:
data = [('a1', 'a2'), ('b1', 'b2')]
Run Code Online (Sandbox Code Playgroud)
我想生成一个这样的字符串: "('a1', 'a2'), ('b1'. 'b2')"
如果我这样做:','.join(data)
,我收到一个错误:
TypeError: sequence item 0: expected string, tuple found
Run Code Online (Sandbox Code Playgroud)
如果我想在一行中做某事而不做以下事情:
for elem in data:
str += ',%s' % str(elem)
Run Code Online (Sandbox Code Playgroud)
那有办法吗?
如何将二进制字符串转换为十进制数?我从值列表中取出二进制文件,然后需要将其转换为十进制格式.
恩.x=["0b000101"]
需要成为x= [5]
这可能吗?
所以我有:
result = subprocess.check_output(['wine',
os.getcwd()+'/static/sigcheck.exe',
'-a','-i','-q',
self.tmpfile.path()])
Run Code Online (Sandbox Code Playgroud)
但每当我运行这个我得到这个错误
CalledProcessError: Command '['wine', '/home/static/sigcheck.exe', '-a', '-i', '-q', '/tmp/tmpxnsN5j']' returned non-zero exit status 1
Run Code Online (Sandbox Code Playgroud)
但如果我check_output
改为call
它工作正常:
Z:\tmp\tmpvOybcm:
Verified: Unsigned
File date: 9:08 AM 10/24/2012
Publisher: Hardcore Computer
Description: Farthest Emitters Converter
Product: Farthest Emitters Converter
Version: 3.2.0
File version: 3.2.0
fixme:mscoree:StrongNameSignatureVerificationEx (L"Z:\\tmp\\tmpvOybcm", 1, 0x33ec13): stub
Strong Name: Unsigned
Original Name: n/a
Internal Name: Farthest Emitters Converter
Copyright: Hardcore Computer 2006
Comments: n/a
Run Code Online (Sandbox Code Playgroud)
有什么理由check_output
不起作用?
python ×7
php ×2
regex ×2
datetime ×1
function ×1
nested-loops ×1
perl ×1
preg-replace ×1
return-value ×1
subprocess ×1
syntax ×1