特定の記号で区切られたテキストファイルを分割するPythonスクリプト
code:cut.py
#!/usr/bin/env python
import os
def makefile(textbody):
print(textbody0)
makefilePath = textbody0 .strip()+ ".txt"
with open(makefilePath, mode='w') as f:
f.writelines(textbody)
filePath ="mmback.txt"
if os.path.exists(filePath):
with open(filePath) as f:
l = f.readlines()
textbody = []
for line in l:
if not line == "---\n":
textbody.append(line)
elif line == "---\n":
print(line + "***")
makefile(textbody)
textbody = []
ファイルmmback.txtが、以下のようになっていたとする
code:mmback.txt
hoge1
内容
内容
内容
---
hoge2
内容
内容
内容
---
hoge3
内容
内容
内容
このときhoge1~hoge3までをタイトルとした三つのテキストファイル作る。
かなり雑なコードなので注意
#Pythonスクリプト