简体   繁体   English

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

[英]How to replace first word of each line in a file with the corresponding element from a list in python?

I have a file like this: 我有一个像这样的文件:

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

I want to read the file line by line and check it like if the first word in the line has ' computer ' in it, then I want to replace the whole first word by 1 , else replace it by -1 . 我想逐行读取文件并检查它是否像该行中的第一个单词中包含“ computer ”,然后我想将整个第一个单词替换为1 ,否则将其替换为-1

I was doing something like this: 我正在做这样的事情:

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)

Now, I get a list: [-1, 1, 1, -1, 1] . 现在,我得到一个列表: [-1, 1, 1, -1, 1] But how do I replace the first words of each line in the file with the corresponding element of this list? 但是,如何用此列表中的相应元素替换文件中每行的第一个单词?

You have to put the whole line together again, after replacing the first part: 在替换了第一部分之后,您必须将整条线重新组合在一起:

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)
content = open('file.txt').readlines()
with open('file.txt', 'w') as flux:
    for line in content:
        first_word = line.split()[0]
        new_word = 'computer' in first_word and '1' or '-1'
        flux.write(line.replace(first_word, new_word))

ps : answer involving the mode 'rw' is not compatible with python3 ps:涉及模式“ rw”的答案与python3不兼容

I would use the classical tempfile usage and process the file line by line : 我将使用经典的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')

Through re module. 通过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)

Output: 输出:

-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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何替换PYTHON列表中每行的第一个元素? - How can I replace the first element of each line in a list in PYTHON? 如何在Python中将文件中的每一行逐字读取到列表中 - How to read each line from a file into list word by word in Python 如何通过不触摸 python 中文本文件的每一行中该单词的 rest 来替换每行中的第一个特定特殊字符 - How to replace the first specific special character in each line by not touching the rest of that word in each of the line of a text file in python 如何替换python中“.txt”文件每一行的第一个数字? - How to replace first number from each line of a ".txt" file in python? "将第一个文件中包含特定单词的每一行替换为第二个文件中的行" - Replace each line containing a specific word from the first file with the line from the second file 替换文件 python 中每一行的第一个字符 - replace first character of each line in file python Python:从 CSV 文件的每一行中提取第一个元素 - Python: Extracting first element from each line of a CSV file 如何创建一个包含文件中每一行的第一个单词的列表(Python 3) - How to create a list that contains the first word of every line in a file (Python 3) 在文件中搜索一个单词,并将其替换为python中字典中的相应值 - search for a word in file and replace it with the corresponding value from dictionary in python Python:如何替换文本文件中一行的最后一个单词? - Python: How to replace the last word of a line from a text file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM