1 回答
TA贡献1829条经验 获得超13个赞
尝试这个:
import os
path = r"D:\test"
token = '-- Name: '
chunks = {}
current_chunk = []
with open (os.path.join(path, "test.txt"), "r") as myfile:
data=myfile.readlines()
for line in data:
if line.startswith(token):
start = line.find("token")+len(token)
end = line.find("(")
schema_name = line[start:end].strip()
current_chunk = []
current_chunk.append(line)
chunks[schema_name] = current_chunk
else:
current_chunk.append(line)
print (chunks)
for name, storage in chunks.items():
print(name)
with open(os.path.join(path, name + '.sql'), 'w') as file:
file.write(" ".join(storage))
file.close()
添加回答
举报