Доброго времени суток!
Подскажите пожалуйста как можно осуществить поиск по файлу и замену найденного, не создавая темповых файлов, и размер файла достаточно велик ~ 15-20Мб?
К примеру, надо в файле заменить name=Vasya на name=Petya.
vladimirseВы на микроволновке запускаете?
о велик ~ 15-20Мб
FishHookНет))) Общий объем памяти 1Gb, но мне всегда казалось, что загружать весь файл в память это плохой тон, ну и как то вообще не делается так.
Вы на микроволновке запускаете?
import re passwd = 'asdad322ed' file = 'file.txt' input_file = open(file, 'r') data = [] for i in input_file.readlines(): if re.findall('password=[\'A-Za-z0-9]+', i): i = 'password=\''+passwd+'\'' data.append(i) input_file.close() output_file = open('ofile.txt', 'w+') for i in data: output_file.writelines(i) output_file.close()
import re pattern = re.compile(r'password=([\'A-Za-z0-9]+)') with open('input.txt') as f_in: passwd_lst = map(pattern.findall, f_in) with open('output.txt', 'w') as f_out: for elem in passwd_lst: map(f_out.write, '\n'.join(elem))
BudulianinНет, я хочу заменить пароль в файле.
По-моему выдирать пароли из строки
vladimirse
import re passwd = 'password=asdad322ed' pattern = re.compile(r'password=([\'A-Za-z0-9]+)') with open('input.txt') as f_in: with open('output.txt', 'w') as f_out: for line in f_in: f_out.write(pattern.sub(passwd, line))
Budulianin
import re
passwd = ‘password=asdad322ed’
pattern = re.compile(r'password+)')
with open('input.txt') as f_in:
with open('output.txt', ‘w’) as f_out:
for line in f_in:
f_out.write(pattern.sub(passwd, line))