我想将一个小型Perl程序重写为Python。我正在按以下方式处理文本文件:
输入:
00000001;Root;;
00000002; Documents;;
00000003; oracle-advanced_plsql.zip;file;
00000004; Public;;
00000005; backup;;
00000006; 20110323-JM-F.7z.001;file;
00000007; 20110426-JM-F.7z.001;file;
00000008; 20110603-JM-F.7z.001;file;
00000009; 20110701-JM-F-via-summer_school;;
00000010; 20110701-JM-F-yyy.7z.001;file;期望产出:
00000001;;Root;;
00000002; ;Documents;;
00000003; ;oracle-advanced_plsql.zip;file;
00000004; ;Public;;
00000005; ;backup;;
00000006; ;20110323-JM-F.7z.001;file;
00000007; ;20110426-JM-F.7z.001;file;
00000008; ;20110603-JM-F.7z.001;file;
00000009; ;20110701-JM-F-via-summer_school;;
00000010; ;20110701-JM-F-yyy.7z.001;file;下面是工作的Perl代码:
#filename: perl_regex.pl
#/usr/bin/perl -w
while(<>) {
s/^(.*?;.*?)(\w)/$1;$2/;
print $_;
} 它从命令行调用它:perl_regex.pl input.txt
对Perl样式regex的解释:
s/ # start search-and-replace regexp
^ # start at the beginning of this line
( # save the matched characters until ')' in $1
.*?; # go forward until finding the first semicolon
.*? # go forward until finding... (to be continued below)
)
( # save the matched characters until ')' in $2
\w # ... the next alphanumeric character.
)
/ # continue with the replace part
$1;$2 # write all characters found above, but insert a ; before $2
/ # finish the search-and-replace regexp.有人能告诉我,如何在Python中获得相同的结果吗?特别是对于$1和$2变量,我找不到相似的东西。
发布于 2014-01-30 14:52:23
Python正则表达式非常类似于Perl的表达式,除了:
r'raw string literal'。\1、\2、.或者\g<1>,\g<2>,。使用re.sub替换。
import re
import sys
for line in sys.stdin: # Explicitly iterate standard input line by line
# `line` contains trailing newline!
line = re.sub(r'^(.*?;.*?)(\w)', r'\1;\2', line)
#print(line) # This print trailing newline
sys.stdout.write(line) # Print the replaced string back.发布于 2014-01-30 14:52:15
python中的替换指令是re.sub(模式,替换,字符串)函数或re.compile(模式).sub(替换,字符串)。在你的情况下,你会这样做:
_re_pattern = re.compile(r"^(.*?;.*?)(\w)")
result = _re_pattern.sub(r"\1;\2", line)注意,$1变成了\1。至于perl,您需要以自己想要的方式迭代行(打开、输入文件、拆分行、.)。
https://stackoverflow.com/questions/21459563
复制相似问题