テキストファイルで指定した文字列の前にあるすべてのテキストを保持(Python)
2317 ワード
class ObstetricsText:
@staticmethod
def __removeAfterI__(key_text, read_path, save_path):
"""
# remove text after key_text, including key_text.
:return:
"""
list_result = []
file = open(read_path, encoding="UTF-8")
for line in file.readlines():
index = line.find(key_text)
if index > 0:
list_result.append(line[0: index])
else:
list_result.append(line.replace("
", ""))
file2 = open(save_path, 'w', encoding="UTF-8")
for line in list_result:
file2.write(line + "
")
file.close()
file2.close()
print("remove success!")
print("File path saved is ", save_file)
if __name__ == '__main__':
read_file = r"C:\Users\Administrator\Desktop\ \ \ \ 10886.txt"
save_file = r"C:\Users\Administrator\Desktop\ \ \ \ 10886_20180501.txt"
ObstetricsText.__removeAfterI__(" :", read_file, save_file)
発生する可能性のある問題: