$$$$
으로 키 값 (예 : db_host
, addons_path
)을 바꿔야합니다.파일의 특정 줄 쓰기 및 바꾸기
#Test.txt#
addons_path=/bin/root
admin_passwd = abctest
auto_reload = False
csv_internal_sep = ,
db_host = 90.0.0.1
출력 텍스트 파일 :
#Test2.txt#
admin_passwd = abctest
auto_reload = False
csv_internal_sep = ,
db_host = $$$$$
나는 새로운 오래된 파일을 대체하는 대신 특정 키의 값을 대체하고 파일에 쓰기 할
입력 텍스트 파일에는 다음이 포함 파일.
다음 함수
나에게 Preplace_with = '7777'
key = 'db_host'
fileref = open('/Files/replace_key/test','r+')
line = fileref.readline()
config = []
while line:
split_line = line.split('=')
if len(split_line) == 2:
config.append((split_line[0].strip(' \n'),split_line[1].strip(' \n')))
print line
line = fileref.readline()
fileref.close()
config = dict(config)
print config
config.update({'db_host':replace_with})
p(config)
로 pprint 수입 pprint에서 fileinput 함수 특정 키 수입의 값을 대체 정확한 출력을 제공하지만 전체 텍스트 파일에 적용 드릴 수 없습니다.
'나오지 -i의/^ DB_HOST = * $/DB_HOST = $ $ $ $ $ /. 'test.txt' –
또는 대안 :'나오지도 -i의 :^\ (DB_HOST | addons_path \). * $ : $ 1 = $$$$ : gm 'test.txt' – hjpotter92