python 提取和修改数据 from/to .txt 文件

python to extract and modify data from/to .txt file

我需要一个由 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放在"dump2.txt"[=58=里面]
  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.
    

我的问题是

我的第一次尝试是:

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 评论时遇到的错误是

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

我不知道如何将正则表达式放在另一个语句中(在 file.write() 中)。 谁能帮忙...

如果我明白你想做什么,你可以用一个循环来表达你的计算

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

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

for line in contents:

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