如何在 python2.7 中使用正则表达式或拆分

how to use regex or split in python2.7

这是一个文件内容,其中有超过 6000 行类似的行:

0000000: 01010000 01001011 00000011 00000100 00010100 00000011  PK....  
0000006: 00000000 00000000 00001000 00000000 01000000 10001101  ....@.  
000000c: 00101001 01000110 10011111 00101100 00000001 11100100  )F.,..  
0000012: 01111100 00101011 00000000 00000000 10111110 11010111  |+....  
0000018: 00000010 00000000 00001101 00000000 00000000 00000000  ......  
000001e: 01110000 01100001 01101110 01100100 01100001 01011111  panda_  
0000024: 01100010 01101001 01101110 00101110 01110100 01111000  bin.tx  
000002a: 01110100 10101100 10011010 01001001 10101110 10011011  t..I..  
0000030: 01010000 00010000 01000101 11100111 01011001 10000101  P.E.Y.  

我需要从每一行中提取部分内容(仅第 2 至 7 列)直到 eof,然后使用 python.

将其放入另一个文件中

第一次尝试只是复制并粘贴一行一行直到eof,.

import StringIO 

infile = "input.txt"
outfile = open("dump.txt", "w")

with open(infile, 'r') as contents:
    line_infile = contents.readline()
    while line_infile:
        outfile.write(line_infile)
        line_infile = contents.readline()
outfile.close()

成功了。

作为第二步我然后在里面添加了're'..这是我做不到的地方。 这是我写的代码:

import StringIO 
import re

infile = "input.txt"
outfile = open("dump.txt", "w")
match = re.compile(ur': (.*?)  ')

with open(infile, 'r') as contents:
    line_infile = contents.readline()
    while line_infile:
        outfile.write(re.findall(match, line_infile))
        line_infile = contents.readline()
outfile.close()

给出错误

outfile.write(re.findall(match, line_infile))
TypeError: expected a character buffer object

尝试使用 re.copy_reg 而不是 re.findall

outfile.write(re.copy_reg(match, line_infile))
TypeError: 'module' object is not callable

我是编程初学者并且 python。根据我目前所学,我必须使用正则表达式来匹配字符串,并使用缓冲区来读取大量数据。我正在使用正则表达式 ': (.*?) ' 来 select 内容 btw 2 个匹配字符,": "(一个 ':' 和一个 Space)和 " "('Space' 和 'Space' )。

问题:

如果您需要的所有内容都只在第 2 至 7 列中,您可以拆分该行,然后只取您需要的元素。

infile = "input.txt"
outfile = open("dump.txt", "w")

with open(infile, 'r') as contents:
    for line in contents:
        line_infile = line.split(' ')[1:7]
        outfile.write(' '.join(line_infile) + '\n')

outfile.close()

试试这个: 使用正则表达式 [^\:]+:([\d\s]+\s\s).*。它每行只给你 2-7 列。并用新行将其展开..

import re
p = re.compile(ur'[^\:]+:([\d\s]+\s\s).*', re.MULTILINE)
test_str = u"0000000: 01010000 01001011 00000011 00000100 00010100 00000011 PK.... \n0000006: 00000000 00000000 00001000 00000000 01000000 10001101 ....@. \n000000c: 00101001 01000110 10011111 00101100 00000001 11100100 )F.,.. \n0000012: 01111100 00101011 00000000 00000000 10111110 11010111 |+.... \n0000018: 00000010 00000000 00001101 00000000 00000000 00000000 ...... \n000001e: 01110000 01100001 01101110 01100100 01100001 01011111 panda_ \n0000024: 01100010 01101001 01101110 00101110 01110100 01111000 bin.tx \n000002a: 01110100 10101100 10011010 01001001 10101110 10011011 t..I.. \n0000030: 01010000 00010000 01000101 11100111 01011001 10000101 P.E.Y. "
subst = u"\n"

result = re.sub(p, subst, test_str)

Live demo

Python 更新.. 现在可能有效

import StringIO 
import re

infile = "input.txt"
outfile = open("dump.txt", "w")
p = re.compile(ur'[^\:]+:([\d\s]+\s\s).*', re.MULTILINE)
subst = u"\n"
with open(infile, 'r') as contents:
    line_infile = contents.readline()
    while line_infile:
        outfile.write(re.sub(p, subst, line_infile))
        line_infile = contents.readline()
outfile.close()