在python中,如何用列表中相应的元素替换文件中每行的第一个字?

在python中,如何用列表中相应的元素替换文件中每行的第一个字?,python,Python,我有这样一个文件: research_t 1:1 2:1 3:1 5:1 13:1 computer_sc 14:1 19:1 21:1 23:1 25:1 26:1 29:1 36:1 44:1 49:1 50:1 51:1 52:1 57:1 68:1 70:1 71:1 75:1 79:1 res_computer 1:1 2:1 3:1 5:1 7:1 8:1 9:1 10:1 11:1 12:1 13:1 15:1 16:1 17:1 18:1 20:1 21:1 22:1 fin

我有这样一个文件:

research_t 1:1 2:1 3:1 5:1 13:1 
computer_sc 14:1 19:1 21:1 23:1 25:1 26:1 29:1 36:1 44:1 49:1 50:1 51:1 52:1 57:1 68:1 70:1 71:1 75:1 79:1 
res_computer 1:1 2:1 3:1 5:1 7:1 8:1 9:1 10:1 11:1 12:1 13:1 15:1 16:1 17:1 18:1 20:1 21:1 22:1
fin_res 23:1 24:1 26:1 28:1 29:1 30:1 32:1 33:1 34:1 35:1 36:1 38:1 39:1 40:1 41:1 43:1 45:1 47:1 48:1 54:1 55:1 56:1 59:1 63:1 64:1 65:1 66:1 72:1 78:1 80:1 
computer_sc_field 2:1 37:1
with open('file.txt', 'r') as f:
    wordlist = [line.split(None, 1)[0] for line in f]
    s = 'computer'
    for i in range(len(wordlist)):
        if s in wordlist[i]:
            wordlist[i] = str(1)
        else:
            wordlist[i] = str(-1)
我想逐行读取文件并检查它,就像如果行中的第一个单词中有“
computer
”,那么我想用
1
替换整个第一个单词,否则用
-1
替换它

我是这样做的:

research_t 1:1 2:1 3:1 5:1 13:1 
computer_sc 14:1 19:1 21:1 23:1 25:1 26:1 29:1 36:1 44:1 49:1 50:1 51:1 52:1 57:1 68:1 70:1 71:1 75:1 79:1 
res_computer 1:1 2:1 3:1 5:1 7:1 8:1 9:1 10:1 11:1 12:1 13:1 15:1 16:1 17:1 18:1 20:1 21:1 22:1
fin_res 23:1 24:1 26:1 28:1 29:1 30:1 32:1 33:1 34:1 35:1 36:1 38:1 39:1 40:1 41:1 43:1 45:1 47:1 48:1 54:1 55:1 56:1 59:1 63:1 64:1 65:1 66:1 72:1 78:1 80:1 
computer_sc_field 2:1 37:1
with open('file.txt', 'r') as f:
    wordlist = [line.split(None, 1)[0] for line in f]
    s = 'computer'
    for i in range(len(wordlist)):
        if s in wordlist[i]:
            wordlist[i] = str(1)
        else:
            wordlist[i] = str(-1)

现在,我得到一个列表:
[-1,1,1,-1,1]
。但是,如何用列表中相应的元素替换文件中每行的第一个单词?

替换第一部分后,您必须重新组合整行:

with open('file.txt', 'r') as file_in:
    with open('output.txt', 'w') as file_out:
        for line in file_in:
            first, other = line.split(None,1)
            line = '%d %s' % (1 if 'computer' in first else -1, other)
            file_out.write(line)

ps:涉及“rw”模式的答案与python3不兼容

我将使用经典的tempfile用法并逐行处理文件:

tmpname = None
with open('file.txt', 'r') as fdin, tempfile.NamedTemporaryFile(
        prefix = 'temp_file', dir='.', delete=False) as fdout:
    tmpname = fdout.name
    for line in fdin:
        first_word, remain = line.split(None, 1)
        fdout.write('1' if 'computer' in first_word else '-1' + ' ' + remain)

os.remove('file.txt')
os.rename(tmpname, 'file.txt')

通过
re
模块

import re
with open('input.txt', 'r') as in_file:
    with open('output.txt', 'w') as out_file:
        for line in in_file:
            if line.startswith('computer'):
                line = re.sub(r'^\S+', r'1', line)
                out_file.write(line)
            else:
                line = re.sub(r'^\S+', r'-1', line)
                out_file.write(line)
输出:

-1 1:1 2:1 3:1 5:1 13:1 
1 14:1 19:1 21:1 23:1 25:1 26:1 29:1 36:1 44:1 49:1 50:1 51:1 52:1 57:1 68:1 70:1 71:1 75:1 79:1 
-1 1:1 2:1 3:1 5:1 7:1 8:1 9:1 10:1 11:1 12:1 13:1 15:1 16:1 17:1 18:1 20:1 21:1 22:1
-1 23:1 24:1 26:1 28:1 29:1 30:1 32:1 33:1 34:1 35:1 36:1 38:1 39:1 40:1 41:1 43:1 45:1 47:1 48:1 54:1 55:1 56:1 59:1 63:1 64:1 65:1 66:1 72:1 78:1 80:1 
1 2:1 37:1

在文本文件中替换是很困难的。如果它很短,那么将整个内容读入内存、编辑它然后将其写回原始文件会容易得多。