[英]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.