python从/ .txt文件中提取和修改数据

时间:2015-01-14 08:54:10

标签: python file edit

我必须使用由xxd command创建的二进制txt文件,我想以特定方式更改二进制内容(某些逻辑位操作)。

这是" 1.txt"

的文件内容
0000000: 01010000 01001011 00000011 00000100 00010100 00000011  PK....

0000006: 00000000 00000000 00001000 00000000 11010111 10111011  ......

000000c: 10010110 01000101 00010011 11010111 01000010 01110110  .E..Bv

0000012: 11011101 00001011 00000000 00000000 10001110 00001110  ......

0000018: 00000000 00000000 00001001 00000000 00000000 00000000  ......

000001e: 01110100 01100101 01110011 01110100 00101110 01100100  test.d

0000024: 01101111 01100011 01111000 10001101 01010111 00000111  ocx.W.

000002a: 01010000 00010011 01101001 00011000 00100101 10110100  P.i.%.

0000030: 01010000 00100101 10000100 00000000 01010010 10100100  P%..R.

0000036: 10000011 01001000 00001111 01000111 10010111 01100010  .H.G.b

000003c: 11101000 11100101 00001110 11101001 10100000 01110100  .....t

0000042: 10100100 11110111 00100110 01000101 10001000 10000000  ..&E..

0000048: 01010010 10010100 10101000 00001000 10000010 00001000  R.....

000004e: 00001010 10000100 01110000 11000010 10100001 10000100  ..p...

我想要的是(步骤如下):

  1. 创建另一个文件(dump.txt)并放置所有二进制内容 从上面的文件如下:

    01010000 01001011 00000011 00000100 00010100 00000011
    
    00000000 00000000 00001000 00000000 11010111 10111011
    
    10010110 01000101 00010011 11010111 01000010 01110110
    
    11011101 00001011 00000000 00000000 10001110 00001110
    
    00000000 00000000 00001001 00000000 00000000 00000000
    
    01110100 01100101 01110011 01110100 00101110 01100100
    
    and so on . . . . . till end of the original(1.txt)
    
  2. 做一些逻辑操作。(这部分已经注意了)例如,将所有二进制值转换为1' s 并将其置于" dump2.txt"
  3. 放置上面步骤(dump2.txt)中的修改内容而不是原始内容 内容。那就是我要编辑原始(1.txt)文件内容 替换从上一步(2)创建的值(来自dump2.txt)。所以 看起来如下..

    0000000: 11111111 11111111 11111111 11111111 11111111 11111111  PK....
    
    0000006: 11111111 11111111 11111111 11111111 11111111 11111111  ......
    
    000000c: 11111111 11111111 11111111 11111111 11111111 11111111  .E..Bv
    
    0000012: 11111111 11111111 11111111 11111111 11111111 11111111  ......
    
    0000018: 11111111 11111111 11111111 11111111 11111111 11111111  ......
    
    000001e: 11111111 11111111 11111111 11111111 11111111 11111111  test.d
    
    0000024: 11111111 11111111 11111111 11111111 11111111 11111111  ocx.W.
    
  4. 我的问题是

    • 使用python如何只将中间部分提取到另一个文件(所以我可以操作内容)。
    • 以及如何将修改后的内容放回同一文件(而不是原始内容)。

    我的第一次尝试是:

    infile = "1.txt"
    outfile = open("dump.txt", "w")
    
    with open(infile, 'r') as contents:
        #for line in contents:
            line = contents.readline()
            for i in range(1,7):
                outfile.write(line.split()[i])
    
    outfile.close()
    

    这个生成的输出为

    010100000100101100000011000001000001010000000011
    

    我知道,第一个for循环不适合一个接一个地获取每一行, 我在nu-commenting时得到的错误是

    ValueError: Mixing iteration and read methods would lose data
    

    我的第二次尝试是:

    import StringIO 
    import re
    
    infile = "2.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
    

    我不知道如何将regex表达式放在另一个语句中(在file.write()中)。 任何人都可以帮忙...

1 个答案:

答案 0 :(得分:0)

如果我理解你要做什么,你可以用一个循环来表达你的计算

for line in contents:
    outfile.write(''.join(line.split()[1:7])

关于取消注释时获得的ValueError,这是因为语句

for line in contents:

表示要从contents读取的一系列行,然后您尝试阅读 来自contents的一行:这会使解释器感到困惑。