그래서 문제를 해결하는 데 어려움이 있습니다. 저는 Python을 처음 사용하기 때문에이 작업을 수행하는 방법을 찾지 못하는 것 같습니다. 내가하려는 것은 파일의 모든 라인을 반복하고 각 라인에 대해 여러 개의 고유 한 규칙을 일치시키고 전체 라인을 사용자 컨텐츠로 대체하는 것입니다. 나는 여러 가지 방법을 시도했지만 둘 중 누구도 필요하지 않습니다.Python 검색 및 바꾸기 라인
print ("File to perform check on:")
fileToSearch = input("> ")
for line in fileinput.input(fileToSearch, inplace=True):
if 'foo' in line :
if 'footimestwo' in line :
sys.__stdout__.write('Wrong Answer. Try again:\n')
textToReplace = input("")
print(line.replace(line, textToReplace), end='')
이렇게하면 줄을 사용자 입력으로 바꿉니다. 그러나 줄을 바꾸지 않습니다. 파일을 새로운 줄로 바꿉니다.
print ("File to perform check on:")
fileToSearch = input("> ")
fin = open(fileToSearch, 'r')
fout = open("newFile.txt", 'w')
for line in fin:
if 'foo' in line :
if 'rom' in line :
print ('Wrong Answer. Try again:')
textToReplace = input("> ")
fout.write(textToReplace+'\n')
else:
fout.write(line)
else:
fout.write(line)
for line in fin:
if 'ram' in line :
if 'foo' in line :
print ('Wrong Answer. Try again:')
textToReplace = input("> ")
fout.write(textToReplace+'\n')
else:
fout.write(line)
else:
fout.write(line)
fin.close()
fout.close()
이 새로운 파일의 변경을하고 첫 번째 문에 대한 라인의 나머지의 상태를 유지하지만 이후 문을 실행하지 않습니다 내가 노력
또 다른 방법이 있었다. 어떤 아이디어?
1 문장으로 요약 하시겠습니까? – smushi
파일의 각 줄을 검색하여 여러 개의 고유 한 기준에 일치 시키려하고 일치하는 경우 사용자가 줄을 변경하고 줄을 사용자 입력으로 바꾸고 다른 줄은 모두 지우도록 요청합니다 변경되었습니다. – JoeFin
ACL에서 보안 검사를 수행하는 데 사용하려고합니다. – JoeFin